简体   繁体   中英

Saving a default value in a Django form

I have a Django template where i'm using two different forms:

views.py

def myview(request):

    if request.method == 'POST':

        form = TradingForm(request.POST)

        if form.is_valid():

            profile = form.save(commit=False)
            profile.user = request.user
            profile.save()
            messages.success(request, f"Success")


    # if a GET (or any other method) we'll create a blank form
    else:
        form = MyForm()


    context = {

        'first_form': FirstForm(request.POST or None),
        'second_form': SecondForm(request.POST or None),
    }

    return render(request, "main/template.html", context)

form.py

  class FirstForm(forms.ModelForm):

        rate = forms.CharField()
        amount = forms.FloatField()


        class Meta:
            model = MyModel
            fields = ("rate", "amount")

        def save(self, commit=True):
            send = super(MyForm, self).save(commit=False)
            if commit:
                send.save()
            return send


  class SecondForm(forms.ModelForm):

        rate = forms.CharField()
        amount = forms.FloatField()


        class Meta:
            model = MyModel
            fields = ("firstfield", "secondfield")

        def save(self, commit=True):
            send = super(MyForm, self).save(commit=False)
            if commit:
                send.save()
            return send

As you can see, those two forms are using the same model, but they have different fields, so the user will submit those fields and the data will be saved in my DB:

class Trade(models.Model):
    amount = models.FloatField()
    rate = models.FloatField()
    formtype = models.CharField(max_length=20)
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, editable=False)

    def save(self): # ALL the signature

        super(Trade, self).save()

The user submits the fields amount and rate . The field user it's not submitted by the user, but it's saved by Django. There is a fourth field, the formtype field. This field must not be submitted by the user, but 'automatically', just like the field user is saved.

An example: if the user used the first form, in my DB, below the formtype column i should see FirstForm , same if the user submitted the data from the second form. I don't know if what i'm trying to accomplish is clear, but i wanted to know how to accomplish this in Django, if there is a way. Every advice is appreciated!

Since you are already overriding save you can add it to the model instance there:

   class FirstForm(forms.ModelForm):
        ...
        def save(self, commit=True):
            send = super(MyForm, self).save(commit=False)
            send.formtype = 'FirstForm'
            if commit:
                send.save()
            return send

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM