Ozeuss
Ozeuss

Reputation: 229

Create forms from model, save to another

I'm building a survey-type app, where the questions are entered/stored in a model (by an admin). I want to use this model to auto-create a user facging form (the response will be stored in another model.

class Question(models.Model):

    question_text = models.CharField(max_length=150)
    questionnaire = models.ForeignKey(Questionnaire) # 'lump' questions together

class Response(models.Model):

    question = models.ForeignKey(Question)
    response_text = models.CharField(max_length=150)
    user = models.IntegerField() # used to record user ID

I'm not sure what's the correct approach - using a ModelForm (populating through init) or a 'regular' form - where I'm not sure how I output my results - using print or adding to a form property.

Thanks.

Upvotes: 0

Views: 31

Answers (1)

bobleujr
bobleujr

Reputation: 1176

In your init you can

self.fields['your_field']=forms.ModelChoiceField(queryset=YourQuerySet.objects.get(pk=X)) // alternatively a filter with first() 

to enforce the initial foreign key object

Upvotes: 1

Related Questions