generics - complicated filtering with GenericForeignKey in django -
i having little hard time figuring 1 out. have 2 custom user models, (for example: buyer, seller) extends single custom user model(customuser) holds common fields(including email
using need filter stuff) , abstract
model.
now, using third party email service send , receive email messages from/to our application. third party service notifies on event. say, if email bounced or failed, send post request on callback
url , after authenticating request update our records , make note of email failed.
we have additional email model(sentmessage) save message sent. model looks this.
class sentmessage(models.model): subject = models.charfield(max_length=100) body = models.textfield() sender = models.foreignkey(models.internaluser) content_type = models.foreignkey(contenttype) object_id = models.positiveintegerfield() recipient = generic.genericforeignkey('content_type', 'object_id') bounced = models.booleanfield(default=false)
as can see, recipient
field in above model genericforeignkey
, can bound other model(in our case buyer or seller). model we'll updating records if message bouced on bounce event etc. looking filter out recipient email address provided third party service. flow like.
--> filter out recipient using given email address.(where recipient can either buyer or seller) --> using above, filter sentmessage
bounced or failed according type of event.
i stuck @ first point here. how can filter object either exists in buyer
model or in seller
model. can't following customuser
class abstract class, both buyer
, seller
inherit:
recipient = models.customuser.objects.get(email=bounced_email)
what best way find(filter) out content_type
, object_id
, recipient
given email address?
this need go https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
first class content_type , object using object_id
recipient = models.customuser.objects.get(email='guido@example.com') messages = sentmessage.objects.filter(content_object=recipient, email=bounced_email)
Comments
Post a Comment