由于ValueError,Django

时间:2018-07-02 15:03:04

标签: python django django-forms

在餐馆在我的平台上注册时,我正在为餐馆节省工作时间,但是,我收到此错误:“ ValueError:save()禁止,以防止由于未保存的相关对象餐馆而导致数据丢失。”

它以前工作正常,在我删除表以开始新迁移后进行了更改。这是错误所指向的位置:

def restaurant_sign_up(request):
    user_form = UserForm(request.POST or None)
    restaurant_form = RestaurantForm(request.POST or None, request.FILES or None)

    if request.method == "POST":
        if user_form.is_valid() and restaurant_form.is_valid():
            new_user = User.objects.create_user(**user_form.cleaned_data)
            new_restaurant = restaurant_form.save(commit=False)

            new_restaurant.user = new_user
            new_restaurant.save()

            login(request, authenticate(
                username = user_form.cleaned_data["username"],
                password = user_form.cleaned_data["password"],

            ))

            for i in range(1, 8):
                OpeningHours.objects.create(
                    restaurant=new_restaurant,
                    day_of_week=i,
                    opening_time="08:00",
                    closing_time="22:00"
                )


            return redirect(restaurant_home)

    return render(request, 'restaurant/signup.html', {
        "user_form": user_form,
        "restaurant_form": restaurant_form,
    # "hours_form": hours_form
    })

回溯指向“ closing_time =“ 22:00”“作为问题。默认情况下,在

时应创建营业时间

这是模型的设置方式:

class OpeningHours(models.Model):
    WEEKDAYS = [
        (1, _("Monday")),
        (2, _("Tuesday")),
        (3, _("Wednesday")),
        (4, _("Thursday")),
        (5, _("Friday")),
        (6, _("Saturday")),
        (7, _("Sunday")),
     ]

    day_of_week = models.IntegerField(choices=WEEKDAYS)
    opening_time = models.TimeField()
    closing_time = models.TimeField()
    restaurant = models.ForeignKey('buddysapp.Restaurant', on_delete=models.CASCADE)

    def __str__(self):
        return self.get_day_of_week_display()

class Restaurant(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='restaurant')
    restaurant_name = models.CharField(max_length=500, blank=False)
    phone = models.CharField(max_length=500)
    logo = models.ImageField(upload_to= 'restaurant_logo/', blank=False)


    #Location
    street_address = models.CharField(default = '',max_length = 50, null = True)
    city = models.CharField(default='', max_length = 254,null=True,blank=True)
    state = models.CharField(default = '',max_length=25, null = True)
    zip_Code = models.IntegerField(null=False, default='')
    lat = models.FloatField(default = "0.0", blank=True)
    lng = models.FloatField(default = "0.0", blank=True)
    latlng = models.CharField(blank=True, max_length=100)



    def __str__(self):
        return self.restaurant_name

     ## Geocode using full address
    def _get_full_address(self):
        return u'%s %s %s %s' % (self.street_address, self.city, self.state, self.zip_Code)
    full_address = property(_get_full_address)


    def super(Restaurant, self).save(*args, **kwargs):
        if not self.latlng:
                    geolocator = Nominatim()
                    location = geolocator.geocode(self.full_address)
                    self.lat = location.latitude
                    self.lng = location.longitude
                    self.latlng = (location.latitude, location.longitude)
        else:
                 location = '+'.join(filter(None, (self.street_address, self.city, self.state, self.zip_Code)))
                 self.latlng = get_lat_lng(location)
                 super(Restaurant, self).save(*args, **kwargs)

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的Restaurant方法不会在super(Restaurant, self).save(*args, **kwargs)分支中调用if not self.latlng。这意味着重凝剂没有保存到数据库,因此在OpeningHours.objects.create(...)中使用它会产生错误。