vendor = Vendor.objects.get(created_by=request.user) you have to pick vendor like this
On Wed, 9 Jun 2021 at 15:43, GWISU MANYANDA <gwisumanya...@gmail.com> wrote: > thank you Lalit for you respond > I have changed this line > > vendor = request.user.vendor > > to > > user = vendor.created_by > > Now i'm getting this error > > NameError at /maduka/vendor_admin/ > > name 'vendor' is not defined > > > On Wed, Jun 9, 2021 at 11:10 AM Lalit Suthar <sutharlalit...@gmail.com> > wrote: > >> coming because of `vendor = request.user.vendor` >> you have attached user to vendor so you can access user from vendor by >> doing >> `user = vendor.created_by` but you can not access vendor from user >> >> On Wed, 9 Jun 2021 at 12:43, GWISU MANYANDA <gwisumanya...@gmail.com> >> wrote: >> >>> Hi Every one! I 'm looking for help. >>> >>> I'm working with a multi vendor website using django. But there is an >>> issue that I have vendor object vendor but it says User has no vendor. Why >>> is this happening and how may I solve this? >>> >>> *MY model* >>> >>> from django.contrib.auth.models import Userfrom django.db import >>> models*class Vendor*(models.Model): >>> name = models.CharField(max_length=255) >>> created_at = models.DateTimeField(auto_now_add=True) >>> created_by = models.OneToOneField(User, related_name='vendor', >>> on_delete=models.CASCADE) >>> class Meta: >>> ordering = ['name'] >>> >>> def __str__(self): >>> return self.name >>> >>> *My View* >>> *def become_vendor(request)*: >>> if request.method == 'POST': >>> form = UserCreationForm(request.POST) >>> if form.is_valid(): >>> user = form.save() >>> login(request, user) >>> vendor = Vendor.objects.create(name=user.username, >>> created_by=user) >>> return redirect('frontpage') >>> >>> else: >>> form = UserCreationForm() >>> return render(request, 'vendor/become_vendor.html', {'form': form}) >>> @login_requireddef vendor_admin(request): >>> vendor = request.user.vendor >>> return render(request, 'vendor/vendor_admin.html', {'vendor': vendor}) >>> >>> *Error i'm getting* >>> RelatedObjectDoesNotExist at /maduka/vendor_admin/ >>> >>> User has no vendor. >>> >>> Request Method: GET >>> Request URL: http://127.0.0.1:8000/maduka/vendor_admin/ >>> Django Version: 3.1.3 >>> Exception Type: RelatedObjectDoesNotExist >>> Exception Value: >>> >>> User has no vendor. >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Django users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to django-users+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/CAKjP07mdfQgHL_hTsXRuUzGTveJJG9VFbTOxQ7ma7DviSCnGaA%40mail.gmail.com >>> <https://groups.google.com/d/msgid/django-users/CAKjP07mdfQgHL_hTsXRuUzGTveJJG9VFbTOxQ7ma7DviSCnGaA%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/CAGp2JVEhHbX6PUPLxqV3AJFRAAwkMYvWcOrDb4z%3DdDVmw14mzQ%40mail.gmail.com >> <https://groups.google.com/d/msgid/django-users/CAGp2JVEhHbX6PUPLxqV3AJFRAAwkMYvWcOrDb4z%3DdDVmw14mzQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAKjP07ngUQvLJfooY4RDBrZevdvdQ9-ir1sfEkvVcRHTinGyew%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAKjP07ngUQvLJfooY4RDBrZevdvdQ9-ir1sfEkvVcRHTinGyew%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGp2JVEdhxdoLyqt%3Ds6U7tBvsng89EXFKBfHbA%3DoAyzQSi%2B8dA%40mail.gmail.com.