I have a question.

Asked 2 years ago, Updated 2 years ago, 46 views

django question.Continue with the previous question .

    class Apply(models.Model):
    writer=models.ForeignKey(Register, on_delete=models.CASCADE,null=True)

I made a registration model using ForeignKey in the model. The ID is optional. The more IDs there are, the more ID of the writer's choice. What I want to make is If I write with the ID A, I want the membership information A of the writing model to be automatically saved, but now the form is saved in the form of selecting the ID, so can I change it?

django

2022-09-21 18:23

1 Answers

Use exclude if you do not want to receive input from the form.

class SomeForm(ModelForm):
    class Meta:
        model = Apply
        exclude = ['writer']

And you can override the save of the form and save it with the id there.

    class Meta:
            model = Apply
            exclude = ['registereturn']
    def save(self, commit=True):
        Apply = super(ApplyForm, self).save(commit=False)
        if commit:
            Apply.register=Register.objects.get(id=1)
            Apply.save()
        return Apply


2022-09-21 18:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.