Reputation: 143
I got an error,AttributeError: 'DeferredAttribute' object has no attribute 'objects'. I wanna parse excel and put it to the model(City&Prefecture&Area&User) . I wrote
user3 = User.objects.filter(corporation_id=val3).first()
if user3:
area = Area.objects.filter(name="America").first()
pref = Prefecture.objects.create(name="prefecture", area=user3.area)
city = City.objects.create(name="city", prefecture=pref)
price_u1000 = Price.upper1000.objects.get(city=city)
price_500_1000 = Price.from500to1000.objects.get(city=city)
price_u500 = Price.under500.objects.get(city=city)
pref.name = "NY"
pref.save()
for i in range(2,len(fourrows_transpose)):
city.name = fourrows_transpose[i][1]
city.save()
print(fourrows_transpose[i][1])
price_u1000.name = fourrows_transpose[i][2]
price_u1000.save()
print(fourrows_transpose[i][2])
price_500_1000.name = fourrows_transpose[i][3]
price_500_1000.save()
print(fourrows_transpose[i][3])
price_u500.name = fourrows_transpose[i][4]
price_u500.save()
print(fourrows_transpose[i][4])
Traceback says this code price_u1000 = Price.upper700.objects.get(city=city)
is wrong.
models.py is
class Area(models.Model):
name = models.CharField(max_length=20, verbose_name='area', null=True)
class User(models.Model):
user_id = models.CharField(max_length=200,null=True)
area = models.ForeignKey('Area',null=True, blank=True)
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='prefecture')
area = models.ForeignKey('Area', null=True, blank=True)
class City(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
prefecture = models.ForeignKey('Prefecture', null=True, blank=True)
class Price(models.Model):
upper1000 = models.CharField(max_length=20, verbose_name='u1000', null=True)
from500to1000 = models.CharField(max_length=20, verbose_name='500~1000', null=True)
under500 = models.CharField(max_length=20, verbose_name='d500', null=True)
city = models.ForeignKey('City', null=True, blank=True)
What should I do to fix this?How should I write it?
Upvotes: 2
Views: 11702
Reputation: 271
Its because upper1000 or what ever field does not have the attribute objects. objects is present in the Model class.
Well if upper1000, from500to1000 and under500 is mutually exclusive you could do something like this. But remember if this is the case you should handle mutually exclusiveness in the models clean function.
price_u1000 = Price.objects.filter(city=city, upper1000__isnull=False, from500to1000__isnull=True, under500__isnull=True)
I would recommend to create a choice field instead of 3 different fields which seems to be mutually exclusive
class Price(models.Model):
UNDER_500 = 'under 500'
FROM_500_TO_1000 = 'from 500 to 1000'
UPPER_1000 = 'upper 1000'
PRICE_CHOICES = [
(UNDER_500, pgettext_lazy('Price under 500')),
(FROM_500_TO_1000, pgettext_lazy('Price from 500 to 1000')),
(UPPER_1000, pgettext_lazy('Price above 1000'))
]
price_range = models.CharField(
verbose_name=ugettext_lazy('Price range'),
max_length=25,
choices=PRICE_CHOICES,
default=UNDER_500
)
city = models.ForeignKey('City', null=True, blank=True)
def clean(self):
super(Price, self).clean()
# implement extra constraints here.
Some readings: https://docs.djangoproject.com/en/1.11/ref/models/querysets/
Upvotes: 2