Ad

Django Model Property Not Showing Up In Object

- 1 answer

so i have the mentioned model down here which I created a property for , for some reason my properties started to NOT show up in my objects

class Cars(models.Model):

datetime = models.DateTimeField()
source = models.CharField(max_length=200)
url = models.CharField(max_length=200)
color = models.CharField(max_length=200,null=True,blank=True)
year_built = models.CharField(max_length=200,null=True,blank=True)
submission = models.CharField(max_length=200,null=True,blank=True)
sell_type = models.CharField(max_length=200,null=True,blank=True)
chassis = models.CharField(max_length=200,null=True,blank=True)
engine = models.CharField(max_length=200,null=True,blank=True)
city = models.CharField(max_length=200,null=True,blank=True)
id = models.CharField(primary_key=True,max_length=200)
title = models.CharField(max_length=200,null=True,blank=True)
insurance_left = models.IntegerField(null=True,blank=True)
body = models.CharField(max_length=200,null=True,blank=True)
gearbox = models.CharField(max_length=200,null=True,blank=True)
mileage = models.BigIntegerField(null=True,blank=True)
price = models.BigIntegerField(null=True,blank=True)
model_year = models.IntegerField(null=True,blank=True)
category = models.CharField(max_length=200,null=True,blank=True)
car = models.CharField(max_length=200,null=True,blank=True)
car_type = models.CharField(max_length=200,null=True,blank=True)
neighborhood = models.CharField(max_length=200,null=True,blank=True)
img = models.CharField(max_length=300,null=True,blank=True)
company = models.CharField(max_length=50,null=True,blank=True)


@property
def letstestproperty(self):
    return 'hello world'




class Meta:
    verbose_name_plural = 'Car Records'

I output my objects using

Cars.objects.values()

but there are no property field added to it

Ad

Answer

The internal behavior of the Django ORM is to return fields on values() It does not include property as they are not fields that map database rows fields(column names). However, if you want all fields then just call all() or first()

Cars.objects.all() or .first()

all() is used to get all objects of the model and first() is used to get the first object from all of the objects of the model at DB level.

Ad
source: stackoverflow.com
Ad