Home > Blockchain >  django form not saving to database(no error message)
django form not saving to database(no error message)

Time:09-03

First of all, I apologize for using a translator because I am not familiar with English.

My Views.py

def my_create_view(request):
    if "reg" in request.method == 'POST':
        first_form = FirstRegForm(request.POST, prefix='firstform')
        second_form = SecondRegForm(request.POST, prefix='secondform')
        if all([first_form.is_valid(), second_form.is_valid()]):
            form = first_form.save(commit=False)
            form.created_name = request.user.user_name
            form.save()
            formm = second_form.save(commit=False)
            formm.shop_seq = form
            formm.save()
            return redirect('someview')
    else:
        first_form = FirstRegForm(prefix='store')
        second_form = SecondRegForm(prefix='input')
    return render(request, 'app/template.html', {'first_form': first_form, 'second_form': second_form})

My Models.py

from django.db.models import Max

class My_user(AbstractBaseUser):
  shop_seq = models.ForeignKey('My_shop', null=True, blank=True, on_delete=models.SET_NULL, db_column="shop_seq")
  user_name = models.CharField(max_length=50)

class My_shop(models.Model):
  shop_seq = models.CharField(primary_key=True, editable=False, max_length=5)
  shop_name = models.CharField(max_length=20, null=True)
  shop_code = models.CharField(max_length=15, null=True, unique=True)
  shop_address = models.CharField(max_length=40, null=True)
  shop_tel = models.CharField(max_length=20, null=True)
  created_name = models.CharField(max_length=100, null=True)

  def save(self, **kwargs):
    if not self.shop_seq:
      max = Rate.objects.aggregate(shop_seq_max=Max('shop_seq'))['shop_seq_max']   1
      self.shop_seq = "{:05d}".format(max if max is not None else 1)
    super().save(*kwargs)

class My_model(models.Model):
  model_id = models.BigAutoField(primary_key=True)
  my_field = models.CharField(max_length=20, null=True)
  some_field = models.CharField(max_length=20, null=True)
  shop_seq = models.ForeignKey('My_shop', on_delete=models.SET_NULL, null=True, db_column="shop_seq", related_name="shop_model")

My Forms.py

class FirstRegForm(forms.ModelForm):
    class Meta:
        model = My_shop
        fields = ('shop_name', 'shop_code', 'shop_address', 'shop_tel',)

class SecondRegForm(forms.ModelForm):
    class Meta:
        model = My_model
        fields = ('my_field', 'some_field',)

My Template.py

<form method="post">{% csrf_token %}
  {{ first_form.shop_name }}
  {{ first_form.shop_code }}
  {{ first_form.shop_address }}
  {{ first_form.shop_tel }}
  {{ second_form.my_field }}
  {{ second_form.some_field }}
  <input  name="reg" type="submit" value="reg">
</form>

If you submit it in this state, it will not be saved in the db.(no error message)

What's wrong with my code?

CodePudding user response:

This line will always be evaluated to false:

if "reg" in request.method == 'POST'

You should change it to:

if request.method == 'POST':
  • Related