我有两个模型,一个是UsersModel
模型,另一个是address_add
。我想在USER_ID字段中创建UsersModel
和address_add
之间的关系。最后,我创建了模板,按下保存按钮后出现此错误。
(1048,“列'USER_ID'不能为空”)
这是我的UsersModel
。
class UsersModel(AbstractBaseUser, PermissionsMixin):
USER_ID = models.AutoField(_('USER_ID'), primary_key=True,)
USER_COMPANY_NAME = models.CharField(_('USER_COMPANY_NAME'), max_length=255, unique=True, blank=True)
USER_EMAIL = models.EmailField(_('USER_EMAIL'), unique=True)
USER_FIRST_NAME = models.CharField(_('USER_FIRST_NAME'), max_length=30, blank=True)
USER_LAST_NAME = models.CharField(_('USER_LAST_NAME'), max_length=30, blank=True)
USER_CREATED_DATE = models.DateTimeField(_('USER_CREATED_DATE'), auto_now_add=True)
USER_MODIFY_DATE = models.DateTimeField(_('USER_MODIFY_DATE'), auto_now=True)
USER_IS_ACTIVE = models.BooleanField(_('USER_IS_ACTIVE'), default=True)
USER_IS_ADMIN = models.BooleanField(_('USER_IS_ADMIN'), default=False)
USER_CITY = models.CharField(_('USER_CITY'), max_length=55, null=True)
USER_GENDER = models.CharField(_('USER_GENDER'), max_length=1, null=True)
objects = UserManager()
USERNAME_FIELD = 'USER_EMAIL'
REQUIRED_FIELDS = []
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
db_table = 'USERS'
def get_full_name(self):
full_name = '%s %s' % (self.USER_FIRST_NAME, self.USER_LAST_NAME)
return full_name.strip()
def get_short_name(self):
return self.USER_FIRST_NAME
def email_user(self, subject, message, from_email=None, **kwargs):
send_mail(subject, message, from_email, [self.USER_EMAIL], **kwargs)
def is_staff(self):
return self.USER_IS_ADMIN
这是我的address_add模式。
class address_add(models.Model):
USER_ID = models.ForeignKey(settings.AUTH_USER_MODEL, db_column='USER_ID',)
ADDRESS_ID = models.AutoField(primary_key=True)
ADDRESS_NAME = models.CharField(max_length=25)
FIRST_NAME = models.CharField(max_length=25)
LAST_NAME = models.CharField(max_length=25)
DELIVERY_ADDRESS = models.TextField(blank=True)
BILLING_ADDRESS = models.TextField(blank=True)
POSTAL_CODE = models.IntegerField()
ADDRESS_CREATED_DATE = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.ADDRESS_NAME
class Meta:
db_table = 'USER_ADDRESS'
这是view.py
def create_address(request):
post = address_add.objects.all()
users = UsersModel.objects.all()
address_form = AddressForm(request.POST or None)
if address_form.is_valid():
address_form.save()
return redirect('/login')
context = {
'address_form': address_form,
'post': post,
'users': users,
}
return render(request, 'accounts/useraddress.html', context)
这是forms.py
class AddressForm(forms.ModelForm):
ADDRESS_NAME = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'name': 'address_name',
'id': 'id_address_name',
'type': 'text',
}))
FIRST_NAME = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'name': 'first_name',
'id': 'id_first_name',
'type': 'text',
}))
LAST_NAME = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'name': 'last_name',
'id': 'id_last_name',
'type': 'text',
}))
DELIVERY_ADDRESS = forms.CharField(widget=forms.Textarea(attrs={
'class': 'form-control',
'name': 'delivery_address',
'id': 'id_delivery_address',
'rows': 4,
'cols': 6,
}))
BILLING_ADDRESS = forms.CharField(widget=forms.Textarea(attrs={
'class': 'form-control',
'name': 'billing_address',
'id': 'id_billing_address',
'rows': 4,
'cols': 6,
}))
POSTAL_CODE = forms.IntegerField(widget=forms.NumberInput(attrs={
'class': 'form-control',
'name': 'postal_code',
'id': 'id_postal_code',
'type': 'number',
}))
class Meta:
model = address_add
fields = [
'ADDRESS_NAME',
'FIRST_NAME',
'LAST_NAME',
'DELIVERY_ADDRESS',
'BILLING_ADDRESS',
'POSTAL_CODE',
]
这是模板。
<form method="POST">
{% csrf_token %}
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">Adres Ekle</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input class="form-control" id="id_user_id" name="USER_ID" type="hidden" value="{{ request.user.USER_ID }}">
</div>
<div class="form-group">
<label for="id_address_name" class="control-label">Adres Adı</label>
{{ address_form.ADDRESS_NAME }}
</div>
<div class="form-group">
<label for="id_first_name" class="control-label">Ad</label>
{{ address_form.FIRST_NAME }}
</div>
<div class="form-group">
<label for="id_last_name" class="control-label">Soyad</label>
{{ address_form.LAST_NAME }}
</div>
<div class="form-group">
<label for="id_delivery_address" class="control-label">Teslimat Adresi</label>
{{ address_form.DELIVERY_ADDRESS }}
</div>
<div class="form-group">
<label for="id_billing_address" class="control-label">Fatura Adresi</label>
{{ address_form.BILLING_ADDRESS }}
</div>
<div class="form-group">
<label for="id_mobile_phone" class="control-label">Telefon</label>
{{ address_form }}
</div>-->
<div class="form-group">
<label for="id_postal_code" class="control-label">Posta Kodu</label>
{{ address_form.POSTAL_CODE }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Vazgeç</button>
<input type="submit" class="btn btn-primary" value="Kaydet">
</div>
</div>
</div>
</div>
</form>
很抱歉长脚本。我在等你的解决方案。 谢谢你们所有人。
答案 0 :(得分:0)
您的address_add
型号USER_ID字段是必需的,您可以通过将null=True
和blank=True
添加到address_add
型号USER_ID字段来更改该字段。
class address_add(models.Model):
...
USER_ID = models.ForeignKey(settings.AUTH_USER_MODEL, db_column='USER_ID', null=True, blank=True)