我有一台paidparking模型
class paidparking(models.Model):
adress = models.ForeignKey(Parking, on_delete=models.SET_NULL, null=True)
carnumber = models.CharField(max_length=150)
amountoftime = models.IntegerField()
price = models.FloatField()
telephone = models.CharField(max_length=20)
email = models.EmailField(,null=True,blank=True )
datetimepaidparking = models.DateTimeField(auto_now_add=True)
expirationdate = models.DateField(null=True)
expirationtime = models.TimeField(null=True)
enddateandtime = models.DateTimeField(null=True,blank=True)模型停车:
class Parking(models.Model):
adress = models.CharField(max_length=150)
starttime = models.TimeField(auto_now=False, auto_now_add=False,null=True)
endtime = models.TimeField(auto_now=False, auto_now_add=False,null=True)
minimaltimeforpayment = models.CharField(max_length=50)
price = models.IntegerField()
numberofavailableseats = models.IntegerField(default=0)
tickets = models.ManyToManyField('tickets', blank=True)我在网站上有一个页面,其中有一个表单。通过此表单,我将数据保存到模型中
class paidparkingForm(forms.ModelForm):
class Meta:
model = paidparking
fields = ['adress','carnumber','amountoftime', 'price', 'email','telephone','expirationdate','expirationtime','enddateandtime']
widgets = {
'adress': forms.Select(attrs={"class": "form-control form", "id": "exampleFormControlSelect1"}),
'carnumber': forms.TextInput(attrs={"class": "form-control form-control-lg form"}),
'amountoftime': forms.NumberInput(attrs={"class": "number form-control form-control-lg form", "disabled": 1}),
'price': forms.NumberInput(attrs={"class": "form-control form-control-lg form", "readonly": 0}),
'email': forms.EmailInput(attrs={"class": "form-control form-control-lg form"}),
'telephone': forms.TextInput(attrs={"class": "form-control form-control-lg form"}),
'expirationdate': forms.DateInput(attrs={"type": "date","class": "form form-control form-control-lg", "disabled": 1}),
'expirationtime': forms.TimeInput(attrs={"type": "time", "class": "form form-control form-control-lg", "disabled": 1}),
'enddateandtime': forms.TextInput(attrs={"class": "form form-control form-control-lg", "readonly": 0}),
}如何将数据直接从函数保存到模型?
我这样做
def save_payment_parking(request):
adress = request.GET["adress"]
carnumber = request.GET["car_number"]
amountoftime = request.GET["amount_of_time"]
price = request.GET["price"]
telephone = request.GET["telephone"]
expirationdate = request.GET["date_time_paid_parking"]
expirationtime = request.GET["expiration_time"]
enddateandtime = request.GET["end_date_and_time"]
save_payment_parking = paidparking
save_payment_parking(adress=adress
,carnumber=carnumber,amountoftime=amountoftime,price=price,telephone=telephone,expirationdate=expirationdate,expirationtime=expirationtime,enddateandtime=enddateandtime).save()但是我得到了一个错误
ValueError: Cannot assign "'26 Balkanskaya Street'": "paidparking.adress" must be a "Parking" instance.我在Parking adress字段的adress字段中插入了一个密钥。如何将我的行插入到地址字段中?停车模型中的地址字段具有我的行的值。也许您需要以某种方式确定记录的id,其中我的字符串等于停车模型的adress字段中的值,并插入此id。
发布于 2021-05-25 23:38:23
在创建paidparking之前,您是否尝试创建Parking
您有一个指向不存在的对象的外键,并且正在尝试将该对象设置为字符串。
def save_payment_parking(request):
adress = request.GET["adress"]
carnumber = request.GET["car_number"]
amountoftime = request.GET["amount_of_time"]
price = request.GET["price"]
telephone = request.GET["telephone"]
expirationdate = request.GET["date_time_paid_parking"]
expirationtime = request.GET["expiration_time"]
enddateandtime = request.GET["end_date_and_time"]
parking = Parking.objects.get_or_create(address=address)[0] # << or something
save_payment_parking = paidparking
save_payment_parking(adress=parking,carnumber=carnumber,amountoftime=amountoftime,price=price,telephone=telephone,expirationdate=expirationdate,expirationtime=expirationtime,enddateandtime=enddateandtime).save()编辑-我添加了objects。检查docs
https://stackoverflow.com/questions/67689182
复制相似问题