首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:无法分配“”26 Balkanskaya Street“”:"paidparking.adress“必须是"Parking”实例

ValueError:无法分配“”26 Balkanskaya Street“”:"paidparking.adress“必须是"Parking”实例
EN

Stack Overflow用户
提问于 2021-05-25 21:39:00
回答 1查看 34关注 0票数 0

我有一台paidparking模型

代码语言:javascript
复制
 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)

模型停车:

代码语言:javascript
复制
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)

我在网站上有一个页面,其中有一个表单。通过此表单,我将数据保存到模型中

代码语言:javascript
复制
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}),
      }

如何将数据直接从函数保存到模型?

我这样做

代码语言:javascript
复制
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()

但是我得到了一个错误

代码语言:javascript
复制
ValueError: Cannot assign "'26 Balkanskaya Street'": "paidparking.adress" must be a "Parking" instance.

我在Parking adress字段的adress字段中插入了一个密钥。如何将我的行插入到地址字段中?停车模型中的地址字段具有我的行的值。也许您需要以某种方式确定记录的id,其中我的字符串等于停车模型的adress字段中的值,并插入此id。

EN

回答 1

Stack Overflow用户

发布于 2021-05-25 23:38:23

在创建paidparking之前,您是否尝试创建Parking

您有一个指向不存在的对象的外键,并且正在尝试将该对象设置为字符串。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67689182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档