4

I have a Phone model that is being constantly used by many different models as a generic relationship. I have no idea how to include it in the Create/Update forms for those models… how good or bad of an idea is it to include the extra fields in a forms.ModelForm subclass… kind of like this:

###### models.py

class UpstreamContactModel(models.Model):
    client = models.ForeignKey(UpstreamClientModel,
            related_name='contacts')
    contact_type = models.CharField(max_length=50, default='Main', 
            blank=True, null=True)
    name = models.CharField(max_length=100, unique=True)
    job_title = models.CharField(max_length=50, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    skype_id = models.CharField(max_length=30, blank=True, null=True)
    phones = generic.GenericRelation(Phone)
    notes = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = 'Contact'


class Phone(models.Model):
    info = models.CharField('Eg. Office, Personal, etc', 
            max_length=15, blank=True)
    number = models.CharField('Phone numbes', max_length=20)
    # generic relationships so I can attach to other objects later on
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    def __unicode__(self):
        return self.number


##### forms.py

class ContactForm(forms.ModelForm,  BaseValidationForm):
    info = forms.CharField(max_length=15)
    number = forms.CharField(max_length=20)

    class Meta:
        model = UpstreamContactModel

    def clean(self):
        ???

    def save(self):
        ???

I've been trying to find out how people handles CRUD when a generic relationship is involved but I've been unsuccessful at that so far.

la_f0ka
  • 1,773
  • 3
  • 23
  • 44
  • 1
    Many years in the future, still looking for a complete example of code answering this question using CBVs... If you were able to implement this it would be great if you could share the solution. – naccode Apr 12 '21 at 15:35
  • I say even using any kind of view. Still searchinf for example how to do it. – Paul Zakharov Jan 21 '22 at 17:10

1 Answers1

1

If you use a model form you can see what kind of form elements they use.

For content_type it will typically use a ModelChoiceField with ContentType.objects.all() as the queryset, and object_id will be a TextInput looking for the positive integer. I don't think the actual generic accessor will show up in the form if you use a model form.

If you want a more elegant solution than this I'd look into writing a custom form field or widget to handle it.

I don't think it's bad practice to add additional fields to the ModelForm as you have, in fact I think that's a good route to go about it. Overriding the save method will probably give you the functionality you desire.

hkothari
  • 254
  • 3
  • 14