Re: Model's Foreign Key Id Attribute not being updated

2017-03-23 Thread Rowan Seymour
I ran into the same problem today in a script for creating large amounts of test data. It would be awkward to re-organize the code it such a way that related objects are assigned to fields only after they've been saved, so I made a utility method to a take a model instance which has fields which

Re: Model's Foreign Key Id Attribute not being updated

2016-11-28 Thread Michael Viveros
Thanks for the detailed responses, they helped clarify things a lot. I will try the first option you mentioned about creating the Questions first (and saving them) and then creating the Choices using the Questions that were just created. My back-up plan will be to use a different pk field for Qu

Re: Model's Foreign Key Id Attribute not being updated

2016-11-28 Thread C. Kirby
On Monday, November 28, 2016 at 3:12:37 PM UTC+2, Michael Viveros wrote: > > Thanks for the reply Kirby. > > I understand that the Question's pk is null when you pass in the Question > to the Choice constructor. But when you save the Choice, the Question was > saved earlier so the Question's pk

Re: Model's Foreign Key Id Attribute not being updated

2016-11-28 Thread Michael Viveros
Thanks for the reply Kirby. I understand that the Question's pk is null when you pass in the Question to the Choice constructor. But when you save the Choice, the Question was saved earlier so the Question's pk is not null. But the Choice's question_id attribute (representing the Question's pk)

Re: Model's Foreign Key Id Attribute not being updated

2016-11-28 Thread C. Kirby
The reason it isn't working in your first example is because the pk gets assigned to the object when it is saved to the database. When you supply question=q to the choice, it is (correctly) getting a pk of null because q has not been saved. On Saturday, November 26, 2016 at 2:56:37 AM UTC+2,

Model's Foreign Key Id Attribute not being updated

2016-11-25 Thread Michael Viveros
I have a Question model and a Choice model where a Question can have many Choices so the Choice model has a Foreign Key mapping to a Question. models.py: from django.db import models class Question(models.Model): text = models.CharField(max_length=200) title = models.CharField(max_leng