Hi, I'm new to django and have been working hard to find "the right way" or at least "a good way" to do things. I'm running into a few problems though that I can't get answers for.
The following post describes my situation very clearly. It may be long-winded but hopefully it's unambiguous. I'm sure there'll be nothing difficult to the experienced django user here, but some answers could be helpful to me and other beginners. In my django model I have the following classes: Person and BankAccount. There are other model types too, but these should suffice to illustrate my problem. Person: person_id first_name surname BankAccount: bank_account_id person_id (ForeignKey) bank_code account_number A Person may have ZERO or ONE BankAccounts. I want to be able to create or edit the details for a Person (including their associated BankAccount, should it exist) on the one html form. I've created some ModelForm types to help with this: class PersonForm(ModelForm): class Meta: model = Person class BankAccountForm(ModelForm): class Meta: model = BankAccount exclude = ('person',) #Avoid the dropdown menu I've been advised (on IRC #django) that "create new" should be handled at one URL and "edit existing" at a separate URL. I handle the URLs at different view functions. So in url.py: url(r'person/add/$', 'person_add', 'person_add'), url(r'person/(?P<person_id>\d+)/$', 'person_edit', name='person_edit'), The "person_add" view function is easy to implement. For request.GET for I simply create a new PersonForm and a new BankAccountForm and expose them to the template via the context. The request.POST is easy too - however I only save the BankAccount data if non-blank data was supplied. The "person_edit" view function raises some problems though: 1) I may be editing a Person that has no associated BankAccount. So basically I'm supplying a form with "edit existing" functionality for the Person model and "create new" functionality for the BankAccount. This doesn't cause a problem in itself, but it does break the paradigm of separating "create new" and "edit existing" functionality - if such a paradigm is indeed correct. 2) When a GET request to /person/5/ is received, I retrieve the relevant Person from the db. In a separate db query I then have to try to get an associated BankAccount. If I do: BankAccount.objects.get(person=person_id) and the BankAccount doesn't exist a DoesNotExist exception is thrown... This really surprised me. Thinking about it from a db query perspective I would have expected an empty list. I notice however if I do: BankAccount.objects.filter(person=person_id) I receive an empty list when there is no match. I presume this is by design, I just wonder what the logic of that design is. Any ideas? 3) When a POST request to /person/5/ is received the URL tells me I'm editing Person "5", but I also need to know if I'm editing or creating a BankAccount too. I can obviously query the BankAccount table for a record matching person_id=5, but again there is a db roundtrip overhead here. Alternatively, I think I could have included a hidden bank_account_id field when I received the GET request for /person/5/ so that upon POST I would "know" the bank_account_id - with some security concerns though. What is best-practice in this situation? 4) You might notice that the relationship between Person and BankAccount is modelled as a One-To-Many, however my business logic requires One-To-(Zero or One). I wondered if I could use the models.OneToOneField for this, but the documentation seemed to imply that django would expect exactly one record on BOTH sides of relationship, so it may not be suitable for the One-To-Zero case. Again, if anyone has advice on what I should actually do here, please let me know. Obviously this is a simple example and I could code a solution that would work. However, my real problem involves several similar relationships on the one form. The POST handling code could get very hairy if I don't do it well. If you got this far, thanks for taking the time to read this. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---