python - Django field refering to another field -
i'm make django model conditions. i've thought these fields
class condition(models.model): model_name = charfield(max_length=100) field_name = charfield(max_length=100) value = charfield(max_length=100)
so can create condition specific field (field_name
) in specific model (model_name
) equal value
. thought there might solution already? instead of using field_name
thought if possible refer directly field either through contenttype
or else?
i know high level , code not model asking. simple examples
django signals determine when change happened. https://docs.djangoproject.com/en/dev/topics/signals/
here signals.py file in app managing person objects when users change:
from django.db import models anotherapp.models import * def user_post_delete(sender, instance, **kwargs): try: if instance.username: person_item = person.objects.get(username = instance.username) person_item.django_user = none person_item.save() except: pass def user_post_save(sender, instance, **kwargs): try: if instance.username: p, created = person.objects.get_or_create( username=instance.username) p.django_user = instance p.save() except: pass models.signals.post_delete.connect(user_post_delete, sender=user) models.signals.post_save.connect(user_post_save, sender=user)
django contenttypes relate condition object in question. https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
here model using contenttypes:
from django.contrib.contenttypes.models import contenttype class comment(models.model): message = models.textfield() #removed fields not relevent content_type = models.foreignkey(contenttype) object_id = models.positiveintegerfield() content_object = generic.genericforeignkey('content_type', 'object_id') date_added = models.datetimefield('date_added',auto_now_add=true, editable=false) date_updated = models.datetimefield('date_updated',auto_now_add=true, auto_now=true, editable=false)
then in views.py/signals.py use contenttype model(ticket model name, , t.id id of ticket object:
new_comment = comment( message = "test message", content_type = contenttype.objects.get(model='ticket'), object_id = t.id, ) new_comment.save()
you want expand have simple boolean if condition met, or bit more , use model store results , come "5
Comments
Post a Comment