On 15 oct, 13:51, jul <juj...@gmail.com> wrote: > Hi, > > I've got the two models Country and Restaurant shown below. Is there a > way to directly set a country by instanciating a restaurant with a > Country instance? Something like: > > r=Restaurant(name='whatever', country=Country(name='newcountry')) > r.save() > > which returns "Column 'country_id' cannot be null"
Indeed - the Country instance only exists in memory by that time. > Or do I have to previously check if the country exists, and creating > it if it doesn't? You may want to read about the Queryset.get_or_create method: http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs In your above exemple, this would be used like: r=Restaurant(name='whatever', country=Country.objects.get_or_create (name='newcountry')[0]) r.save() --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---