32

I have two models in Django that are related with a OneToOneField (PrinterProfile and PrinterAdress). I am trying to do a form with PrinterProfileForm, but for some reason it does NOT pass the PrinterAddress fields into the form (it's not rendered by Django "magic" in the template).

What should I do so that my PrinterProfileForm include as well the fields from PrinterAddress (its related OneToOneField)?

Thanks a lot

class PrinterProfile(TimeStampedModel):
    user = models.OneToOneField(User)
    phone_number = models.CharField(max_length=120, null=False, blank=False)
    additional_notes = models.TextField()
    delivery = models.BooleanField(default=False)
    pickup = models.BooleanField(default=True)


# The main address of the profile, it will be where are located all the printers.    
class PrinterAddress(TimeStampedModel):
    printer_profile = models.OneToOneField(PrinterProfile)
    formatted_address = models.CharField(max_length=200, null=True)
    latitude = models.DecimalField(max_digits=25, decimal_places=20)  # NEED TO CHECK HERE THE PRECISION NEEDED.
    longitude = models.DecimalField(max_digits=25, decimal_places=20)  # NEED TO CHECK HERE THE PRECISION NEEDED.
    point = models.PointField(srid=4326)

    def __unicode__(self, ):
        return self.user.username

class PrinterProfileForm(forms.ModelForm):
    class Meta:
        model = PrinterProfile
        exclude = ['user']
Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
jhagege
  • 1,486
  • 3
  • 22
  • 36

2 Answers2

46

You have to create second form for PrinterAddress and handle both forms in you view:

if all((profile_form.is_valid(), address_form.is_valid())):
    profile = profile_form.save()
    address = address_form.save(commit=False)
    address.printer_profile = profile
    address.save()

Of course in the template you need to show both forms under one <form> tag :-)

<form action="" method="post">
    {% csrf_token %}
    {{ profile_form }}
    {{ address_form }}
</form>
catavaran
  • 44,703
  • 8
  • 98
  • 85
  • Thank you so much! Life-saver... Any idea why it's not taken care automatically by Django ModelForm? Is it a design decision or some kind of bug? Thanks – jhagege Jan 08 '15 at 02:50
  • 1
    This is design decision. There is a lot of pitfalls in handling more than one model in single form (for example: how to handle model relations, how to handle fields with the same name in different models, how to set additional fields outside of the form). – catavaran Jan 08 '15 at 02:59
  • Makes sense, though I understand that it works with ForeignKeys (inline_formset), so what is the difference with onetooneField? It sounds less tricky to me, isn't it? – jhagege Jan 08 '15 at 03:01
  • No, it doesn't work with inline formset. Each form in formset handles only one "inline" model. – catavaran Jan 08 '15 at 03:18
3

Complementing the accepted answer:

If you have custom clean methods, you need to add a try/except case. For the example presented if address had a clean() method to validate something you needed to change it to:

def clean(self):
    try:
        printer_profile = self.printer_profile 
    except ObjectDoesNotExist:
        pass
    else:
        ...code to validate address...
Community
  • 1
  • 1
NBajanca
  • 3,544
  • 3
  • 26
  • 53