user10086707
user10086707

Reputation:

Q.DjangoRestFramework (Cannot resolve keyword 'ContentType' into field)

I have models.py looks like this:

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)

And I have added Student as Shown Below:

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']
                                        }
                                        )

And When I try to get the list of student it show error like this

Cannot resolve keyword 'ContentType' into field. Choices are: address, city, content_object, content_type, content_type_id, date_created, date_deleted, date_updated, district, id, object_id, province

Can anyone help me > I hope U understand my Question. My list function is like this

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)

Upvotes: 1

Views: 85

Answers (1)

Enthusiast Martin
Enthusiast Martin

Reputation: 3091

Problem is here:

address = Address.objects.get(ContentType=c,object_id=user.id)

It should be:

address = Address.objects.get(content_type=c,object_id=user.id)

ContentType is not the field you use in your Address model - it is content_type.

ContentType is the actual model class for your content type.

Upvotes: 2

Related Questions