代码之家  ›  专栏  ›  技术社区  ›  Doing Things Occasionally

由于ValueError、Django,无法保存新用户

  •  -1
  • Doing Things Occasionally  · 技术社区  · 6 年前

    当餐厅在我的平台上注册时,我会将营业时间节省到餐厅,但是,我得到了一个错误:“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 回复  |  直到 6 年前
        1
  •  1
  •   Alasdair    6 年前

    你的 Restaurant 方法不调用 super(Restaurant, self).save(*args, **kwargs) if not self.latlng 分支。这意味着重新分配不会保存到数据库中,因此在 OpeningHours.objects.create(...) 会出错。