Chet tugmacha bilan namunaviy namunani yaratishda nima uchun django xatolarini olishim kerak?

Men quyidagi xatolarni qabul qilaman:

ValueError: Cannot assign "u'ben'": "Entry.author" must be a "MyProfile" instance.

Ushbu qatordan:

form.author = request.session['username']

Eslatma: Entry.author, quyida ko'rsatilgan tashqi kalit.

models.py

class MyProfile(models.Model):
    user = models.CharField(max_length=16)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    def __unicode__(self):
        return u'%s %s %s' % (self.user, self.firstname, self.lastname)

class Entry(models.Model):
    headline= models.CharField(max_length=200,)
    body_text = models.TextField()
    author=models.ForeignKey(MyProfile, related_name='entryauthors')

    def __str__(self):
        return u'%s %s %s' % (self.headline, self.body_text, self.author)

Xato, Entry.author "MyProfile" misoli bo'lishi kerak, lekin django qobig'iga kirib, so'rovni ishga tushirganda men foydalanuvchi nomi bilan bir misol mavjudligini ko'rsam.

]>

Iltimos, so'raganingizni so'rayapsizmi? ['Username'] to'g'ri formatlangan foydalanuvchi nomini qaytarmasligi mumkin va men buni bilishim mumkin emas (men buni bilaman) django qobig'ida, chunki siz so'rovdan foydalanishingiz mumkin deb o'ylamayman qobiqdan ob'ekt.

Kirish shaklida, foydalanuvchi so'rovini so'rovga yuboradigan ushbu satr bor.

if form.is_valid():
    username = form.cleaned_data['username']
    request.session['username'] = username
0
Oh, bu ob'ekt bo'lishi kerakligini bilmasdim, batamom. Rahmat!
qo'shib qo'ydi muallif david, manba
Oh, bu ob'ekt bo'lishi kerakligini bilmasdim, batamom. Rahmat!
qo'shib qo'ydi muallif david, manba
request.session ['username'] - bu mag'lubiyat bo'lsa, form.author MyProfile namunasi bo'lishi kerak. form.author = MyProfile.objects.get (username = request.session ["username"]) kabi muallifni tanlash bilan so'rovlar qilishingiz kerak bo'lishi mumkin
qo'shib qo'ydi muallif Ahmed Hosny, manba
request.session ['username'] - bu mag'lubiyat bo'lsa, form.author MyProfile namunasi bo'lishi kerak. form.author = MyProfile.objects.get (username = request.session ["username"]) kabi muallifni tanlash bilan so'rovlar qilishingiz kerak bo'lishi mumkin
qo'shib qo'ydi muallif Ahmed Hosny, manba
request.session ['username'] - bu mag'lubiyat bo'lsa, form.author MyProfile namunasi bo'lishi kerak. form.author = MyProfile.objects.get (username = request.session ["username"]) kabi muallifni tanlash bilan so'rovlar qilishingiz kerak bo'lishi mumkin
qo'shib qo'ydi muallif Ahmed Hosny, manba
Buni javob sifatida qo'shimcha tafsilotlar bilan e'lon qildim.
qo'shib qo'ydi muallif Ahmed Hosny, manba
Buni javob sifatida qo'shimcha tafsilotlar bilan e'lon qildim.
qo'shib qo'ydi muallif Ahmed Hosny, manba

6 javoblar

form.author must be a MyProfile instance while request.session['username'] is a string. You may need to do a query selecting the Author such as form.author = MyProfile.objects.get(user=request.session['username'])

DoesNotExist xatosini oldini olish uchun quyidagi amallarni bajarishingiz mumkin:

try:
   form.author = MyProfile.objects.get(user=request.session['username'])
except MyProfile.DoesNotExist:
    form.author = None

Side Eslatma: MyProfile modelida foydalanuvchi nomi yo'q, shuning uchun uni nom bilan atashadi

0
qo'shib qo'ydi

form.author must be a MyProfile instance while request.session['username'] is a string. You may need to do a query selecting the Author such as form.author = MyProfile.objects.get(user=request.session['username'])

DoesNotExist xatosini oldini olish uchun quyidagi amallarni bajarishingiz mumkin:

try:
   form.author = MyProfile.objects.get(user=request.session['username'])
except MyProfile.DoesNotExist:
    form.author = None

Side Eslatma: MyProfile modelida foydalanuvchi nomi yo'q, shuning uchun uni nom bilan atashadi

0
qo'shib qo'ydi

form.author must be a MyProfile instance while request.session['username'] is a string. You may need to do a query selecting the Author such as form.author = MyProfile.objects.get(user=request.session['username'])

DoesNotExist xatosini oldini olish uchun quyidagi amallarni bajarishingiz mumkin:

try:
   form.author = MyProfile.objects.get(user=request.session['username'])
except MyProfile.DoesNotExist:
    form.author = None

Side Eslatma: MyProfile modelida foydalanuvchi nomi yo'q, shuning uchun uni nom bilan atashadi

0
qo'shib qo'ydi

Buni ishlatganimdagina, "chet el kalit" har bir sinfda birgalikda bir ob'ekt joylashgan bo'lishi kerak (iltimos, men o'zim o'qitadigan teshik terminologiyasini avf qiling).

Misol kodi shunga o'xshash:

class One():
    key1 = models.CharField(max_length=150)

class Two():
    key1 = models.ForeignKey(key1)

Umid qilamanki bu yordam beradi.

https://github.com/Ry10p/django-Plugis/blob/master/courses/models.py line 52

- Cheers

0
qo'shib qo'ydi
qo'shib qo'ydi muallif Ry10p, manba

Buni ishlatganimdagina, "chet el kalit" har bir sinfda birgalikda bir ob'ekt joylashgan bo'lishi kerak (iltimos, men o'zim o'qitadigan teshik terminologiyasini avf qiling).

Misol kodi shunga o'xshash:

class One():
    key1 = models.CharField(max_length=150)

class Two():
    key1 = models.ForeignKey(key1)

Umid qilamanki bu yordam beradi.

https://github.com/Ry10p/django-Plugis/blob/master/courses/models.py line 52

- Cheers

0
qo'shib qo'ydi
qo'shib qo'ydi muallif Ry10p, manba

Buni ishlatganimdagina, "chet el kalit" har bir sinfda birgalikda bir ob'ekt joylashgan bo'lishi kerak (iltimos, men o'zim o'qitadigan teshik terminologiyasini avf qiling).

Misol kodi shunga o'xshash:

class One():
    key1 = models.CharField(max_length=150)

class Two():
    key1 = models.ForeignKey(key1)

Umid qilamanki bu yordam beradi.

https://github.com/Ry10p/django-Plugis/blob/master/courses/models.py line 52

- Cheers

0
qo'shib qo'ydi
qo'shib qo'ydi muallif Ry10p, manba