0
我得到的錯誤:爲什麼我會得到AttributeError:'User'對象沒有屬性'zipcode'?
AttributeError: 'User' object has no attribute 'zipcode'
當用戶填寫表單賣一個項目我得到這個錯誤。
這裏是我的觀點:
def get_entry(request):
if request.method == 'POST':
f = SellForm(request.POST)
if f.is_valid():
form=f.save(commit=False)
form.author = request.user
form.zipcode = request.user.zipcode
form.pubdate = datetime.datetime.now
form.save()
else:
f = SellForm()
return render(request, 'sell.html', {'form': f})
這裏是我的模型:
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from userena.models import UserenaBaseProfile
from django.conf import settings
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User,
unique=True,
verbose_name=_('user'),
related_name='my_profile')
city = models.TextField(null=True, blank=True)
state = models.TextField(null=True, blank=True)
zipcode = models.IntegerField(_('zipcode'),
max_length=5, null=True, blank=True)
coverpic = models.ImageField(upload_to="/site_media/media/covers/", null=True, blank=True)
class Entry(models.Model):
headline= models.CharField(max_length=200,)
body_text = models.TextField()
author=models.ForeignKey(settings.AUTH_USER_MODEL, related_name='entryauthors')
pub_date=models.IntegerField(max_length=8)
zipcode =models.ForeignKey(settings.AUTH_USER_MODEL, related_name='entryzipcodes')
price1 = models.TextField()
price2 = models.TextField()
item_picture = models.ImageField(upload_to="/site_media/media/items/", null=True, blank=True)
這裏是我的形式:
class SellForm(ModelForm):
class Meta:
model = Entry
fields = ['headline', 'body_text', 'author', 'pub_date', 'zipcode', 'price1', 'price2', 'item_picture']
我做什麼毛病模式?
你在哪裏得到這個錯誤? – 2014-10-11 20:24:34
'zipcode'是'MyProfile'的屬性。 – 2014-10-11 20:25:17
我已經更新了這個問題。我懷疑我在get_entry視圖方法中做錯了什麼。 – stephan 2014-10-11 20:27:58