Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can implement this using generic relationships by manually creating the junction table between message and recipient:</p> <pre><code>from django.db import models from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType class Client(models.Model): city = models.CharField(max_length=16) # These aren't required, but they'll allow you do cool stuff # like "person.sent_messages.all()" to get all messages sent # by that person, and "person.received_messages.all()" to # get all messages sent to that person. # Well...sort of, since "received_messages.all()" will return # a queryset of "MessageRecipient" instances. sent_messages = generic.GenericRelation('Message', content_type_field='sender_content_type', object_id_field='sender_id' ) received_messages = generic.GenericRelation('MessageRecipient', content_type_field='recipient_content_type', object_id_field='recipient_id' ) class Meta: abstract = True class PersonClient(Client): first_name = models.CharField(max_length=16) last_name = models.CharField(max_length=16) gender = models.CharField(max_length=1) def __unicode__(self): return u'%s %s' % (self.last_name, self.first_name) class CompanyClient(Client): name = models.CharField(max_length=32) tax_no = models.PositiveIntegerField() def __unicode__(self): return self.name class Message(models.Model): sender_content_type = models.ForeignKey(ContentType) sender_id = models.PositiveIntegerField() sender = generic.GenericForeignKey('sender_content_type', 'sender_id') msg_body = models.CharField(max_length=1024) def __unicode__(self): return u'%s...' % self.msg_body[:25] class MessageRecipient(models.Model): message = models.ForeignKey(Message) recipient_content_type = models.ForeignKey(ContentType) recipient_id = models.PositiveIntegerField() recipient = generic.GenericForeignKey('recipient_content_type', 'recipient_id') def __unicode__(self): return u'%s sent to %s' % (self.message, self.recipient) </code></pre> <p>You'd use the above models like so:</p> <pre><code>&gt;&gt;&gt; person1 = PersonClient.objects.create(first_name='Person', last_name='One', gender='M') &gt;&gt;&gt; person2 = PersonClient.objects.create(first_name='Person', last_name='Two', gender='F') &gt;&gt;&gt; company = CompanyClient.objects.create(name='FastCompany', tax_no='4220') &gt;&gt;&gt; company_ct = ContentType.objects.get_for_model(CompanyClient) &gt;&gt;&gt; person_ct = ContentType.objects.get_for_model(person1) # works for instances too. # now we create a message: &gt;&gt;&gt; msg = Message.objects.create(sender_content_type=person_ct, sender_id=person1.pk, msg_body='Hey, did any of you move my cheese?') # and send it to a coupla recipients: &gt;&gt;&gt; MessageRecipient.objects.create(message=msg, recipient_content_type=person_ct, recipient_id=person2.pk) &gt;&gt;&gt; MessageRecipient.objects.create(message=msg, recipient_content_type=company_ct, recipient_id=company.pk) &gt;&gt;&gt; MessageRecipient.objects.count() 2 </code></pre> <p>As you can see, this is a far more verbose (complicated?) solution. I'd probably keep it simple and go with Prariedogg's solution above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload