У меня models.py выглядит так:
class StudentAdmission(BaseModel):
student = models.ForeignKey(Student,on_delete=models.CASCADE)
admission_date = models.DateTimeField(auto_now_add=True)
batch = models.IntegerField()
course = models.ForeignKey(Course,on_delete=models.CASCADE)
description = models.CharField(max_length=120)
class Student(BaseModel):
user = models.ForeignKey(User,on_delete=models.CASCADE)
registration_no = models.IntegerField()
class User(BaseModel, AbstractUser):
type = models.IntegerField(choices=USER_TYPE,null=True)
gender = models.IntegerField(choices=GENDER,null=True)
TYPE =(
('PHONE',1),
('LANDLINE',2),
('CDMA',3),
)
class Phone(BaseModel):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
object_id = models.PositiveIntegerField(null=True)
content_object = fields.GenericForeignKey('content_type', 'object_id')
type = models.IntegerField(choices=TYPE)
number = models.IntegerField()
class Address(BaseModel):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
object_id = models.PositiveIntegerField(null=True)
content_object = fields.GenericForeignKey('content_type', 'object_id')
province = models.CharField(max_length=120)
district = models.CharField(max_length=120)
city = models.CharField(max_length=120)
И я добавил Студента, как показано ниже:
Views.py:
def create(self,request):
----------
...lots of code ...
user,b = User.objects.get_or_create(
email=ud['email'],
defaults={
'username':ud['email'],
'first_name':ud['first_name'],
'last_name':ud['last_name'],
'gender':ud['gender'],
'type':ud['type']
}
)
if not b:
raise serializers.ValidationError({
'detail':["Email Already Exist"]
})
c = ContentType.objects.get_for_model(user)
Phone.objects.get_or_create(content_type=c,
object_id=user.id,
number=data['phone_detail']['number'],
type=data['phone_detail']['type']
)
Address.objects.get_or_create(
content_type=c,object_id=user.id,
defaults={
'province':data['address_detail']['province'],
'district':data['address_detail']['district'],
'city':data['address_detail']['city'],
'address':data['address_detail']['address']
}
)
И когда я пытаюсь получить список учеников, появляется сообщение об ошибке, подобное этому
Невозможно разрешить ключевое слово ContentType в поле. Возможные варианты: адрес, город, content_object, content_type, content_type_id, date_created, date_deleted, date_updated, район, id, object_id, провинция
Может ли кто-нибудь мне помочь> Я надеюсь, вы понимаете мой вопрос.
Мой список функций выглядит так
def list(self...):
;''''''lots of code...
queryset = Student.objects.all()
def list(self,request):
output = []
for adm in StudentAdmission.objects.all():
user = adm.student.user
c=ContentType.objects.get_for_model(user)
try:
address = Address.objects.get(ContentType=c,object_id=user.id)
print(address)
except Exception as E:
print(E)
tmp ={
'province':address.district
}
return ResPonse(tmp)