通过API调用保存FieldIn Django

时间:2018-07-21 02:00:23

标签: python django api

每当调用api时我都试图创建一个新的CustomerDetail对象。但是问题是,每当我尝试为喜欢的饭菜多选择字段创建某些东西时,都会出现此错误:

MultiValueDictKeyError at /api/customer/favorite_meal/
"'favorite_meal'"

这是api:

@csrf_exempt
def favorite_meal(request):

    if request.method == "POST":

      access_token = AccessToken.objects.get(token = request.GET.get("access_token"),
      expires__gt = timezone.now())

      customer = access_token.user.customer

    details = CustomerDetailSerializer(
            CustomerDetails.objects.create(
            customer = customer,
            favorite_mean = request.POST["favorite_meal"]
            ))

    return JsonResponse({"status" : "success"})

这是我的模特:

class CustomerDetails(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='customer')

    TYPE = (
    ('Time', (
        ('Breakfast', 'Breakfast'),
        ('Lunch', 'Lunch'),
        ('Dinner', 'Dinner'),
            )),
        )
   
    favorite_meal = MultiSelectField(choices=TYPE)
    interested_in = MultiSelectField(choices=CATEGORIES)
    likes = models.ManyToManyField('Product')
    completed_orders = models.IntegerField(default = "0", blank=True)
    shares = models.IntegerField(default = "0", blank=True)
    average_order = models.FloatField(default = "0.0", blank=True)

    def __str__(self):
        return self.customer.user.get_full_name()

在请求中,我试图通过api选择早餐和午餐。我该怎么办?

发送参数:

状态表示成功,但是没有详细的客户数据

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试从>访问static double money;//must be a global variable so it could be accessed from all methods. //you declared it in verse2() method what means that you can ONLY access it in verse2(). public static void main(String[] args) { verse1(); System.out.println(); verse2(); System.out.println(); verse3(); System.out.println(); verse4(); } public static void verse1() { System.out.println("Welcome to Shoreline's Computer Candy Machine!"); System.out.println("(All candy provided is virtual.)"); } public static void verse2() { Scanner console = new Scanner(System.in); System.out.print("How much money do you have? >"); //prompts for a whole number money = console.nextDouble(); System.out.printf("%.2f, that's all?", money); } public static void verse3() { System.out.println("Well, let me tell you what we got here."); System.out.println("A $0.65 Twix"); System.out.println("B $0.50 Chips"); System.out.println("C $0.75 Nutter Butter"); System.out.println("D $0.65 Peanut Butter Cup"); System.out.println("E $0.55 Juicy Fruit Gum"); } public static void verse4() { Scanner input = new Scanner(System.in); System.out.print("So, What do you want? >"); //prompts for a whole number String a = input.next(); double change = 0;//the amount of change to give back. //check which candy they picked as well as if the money is equal or larger than the price. //not the >= is very important, you only had >. if (a.equals("A") && money >= 0.65) { change = money - 0.65;//calculate the change by doing the money - price of candy. System.out.println("Thanks for purchasing candy through us."); System.out.println("Please take your candy and your $" + change + " change!"); } else if (a.equals("B") && money >= 0.50) {//same thing for item B, and check the price change = money - 0.50; System.out.println("Thanks for purchasing candy through us."); System.out.println("Please take your candy and your $" + change + " change!"); }//now do items C,D,E in with the same logic. //now you need to make sure that maybe the user doesn't have enough money... //you would you the else{...} to prompt to user that the money is not enough. } 时出现MultiValueDictKeyError错误,但favorite_meal中不存在request.POST 试试这个

favorite_meal


屏幕截图 Postamn screenshot



更新

request.POST