mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-03-16 22:40:26 +00:00
Contribute genericrelation helper to entity on registration
This commit is contained in:
parent
d989e85223
commit
b765aa9c51
1 changed files with 15 additions and 1 deletions
16
utils.py
16
utils.py
|
|
@ -1,11 +1,13 @@
|
|||
from django.db.models.signals import post_init, post_save
|
||||
from django.contrib.contenttypes import generic
|
||||
from .managers import EntityManager
|
||||
from .models import EavEntity, EavAttribute
|
||||
from .models import EavEntity, EavAttribute, EavValue
|
||||
|
||||
class EavConfig(object):
|
||||
|
||||
proxy_field_name = 'eav'
|
||||
manager_field_name ='objects'
|
||||
generic_relation_field_name = 'eav_values'
|
||||
|
||||
@classmethod
|
||||
def get_eav_attributes(cls):
|
||||
|
|
@ -60,6 +62,13 @@ class EavRegistry(object):
|
|||
mgr = EntityManager()
|
||||
mgr.contribute_to_class(model_cls, config_cls.manager_field_name)
|
||||
|
||||
gr_name = config_cls.generic_relation_field_name
|
||||
generic_relation = generic.GenericRelation(EavValue,
|
||||
object_id_field='entity_id',
|
||||
content_type_field='entity_ct')
|
||||
generic_relation.contribute_to_class(model_cls, gr_name)
|
||||
|
||||
|
||||
|
||||
@staticmethod
|
||||
def unregister(model_cls):
|
||||
|
|
@ -81,6 +90,11 @@ class EavRegistry(object):
|
|||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
delattr(model_cls, config_cls.generic_relation_field_name)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if 'old_mgr' in cache:
|
||||
cache['old_mgr'].contribute_to_class(model_cls,
|
||||
config_cls.manager_field_name)
|
||||
|
|
|
|||
Loading…
Reference in a new issue