我已经检查了几十个例子,我认为我正在以正确的方式进行,但是我收到此错误消息'City'对象没有属性'store',请帮忙,看看我附带的序列化器。
class CitySerializer(serializers.HyperlinkedModelSerializer):
store = serializers.HyperlinkedRelatedField(view_name = 'store:listStoreByCity',read_only=True)
class Meta:
model = City
read_only_fields = ['location']
fields = [
"city",
"latitude",
"longitude",
"store",
"state",
"img",
"location",
]
Models.py
from django.contrib.gis.db import models
from localflavor.us.us_states import STATE_CHOICES
class City(models.Model):
city = models.CharField(max_length=120)
latitude = models.CharField(blank=True, max_length=11, default=0)
longitude = models.CharField(blank=True, max_length=11, default=0)
state = models.CharField(max_length=2, choices=STATE_CHOICES, null=True, blank=True)
img = models.ImageField(upload_to='img', blank=True)
location = models.PointField(null=True, blank=True)
def __str__(self):
return str(self.city)
答案 0 :(得分:0)
默认情况下,DRF会通过您在模型中没有的 #include "stdafx.h"
#include <iostream>
#include <string.h>
#include <string>
// Required headers
using namespace std;
//Class movie starts
class Movie {
public:
void SetMovieName(string moviename);
// Function to set the name of the movie
// moviename = movie; later on
string GetMPAAR();
//function to return the MPAA rating
int amountofratingsof1() const;
//function to return the number of people that rated the movie as a 1
int amountofratingsof2() const;
//function to return the number of people that rated the movie as a 2
int amountofratingsof3() const;
//function to return the number of people that rated the movie as a 3
int amountofratingsof4() const;
//function to return the number of people that rated the movie as a 4
int amountofratingsof5() const;
//function to return the number of people that rated the movie as a 5
int average() const;
//function to return the average value of all ratings
std::string Movies(string moviename = "Gavecube: The Movie");
//constructor to set the movie
private:
string Movie; //variable to store the name of the movie
int peoplethatrated1; // variable to store the number of people that rated 1
int peoplethatrated2; // variable to store the number of people that rated 2
int peoplethatrated3; // variable to store the number of people that rated 3
int peoplethatrated4; // variable to store the number of people that rated 4
int peoplethatrated5; // variable to store the number of people that rated 5
};
//implementation file:
void Movie::SetMovieName(const string moviename) {
//function below checks if it is a string or not
if (!cin) {
cout << "Not a valid input. Please restart." << endl;
}
}
int Movie::amountofratingsof1()const {
}
int Movie::amountofratingsof2()const {
}
int Movie::amountofratingsof3()const {
}
int Movie::amountofratingsof4()const {
}
int Movie::amountofratingsof5()const {
}
//constructor
std::string Movie(string moviename) {
SetMovieName(moviesname)
}
int main()
{
Movie Movies("Hello");
return 0;
}
名称查看相关对象。
将related_name设置为“store”或在序列化程序的store
中使用源序列化程序的字段的参数源,并将其设置为store
。
如果您仍不清楚,请查看教程http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#adding-information-to-our-model中描述的这些案例