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 User
from 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_required def vendor_admin(request): vendor = request.user.vendor return render(request, 'vendor/vendor_admin.html', {'vendor': vendor})
Error i'm getting
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKjP07mdfQgHL_hTsXRuUzGTveJJG9VFbTOxQ7ma7DviSCnGaA%40mail.gmail.com.
vendor = request.user.vendor
to
user = vendor.created_by
Now i'm getting this error
name 'vendor' is not defined
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGp2JVEhHbX6PUPLxqV3AJFRAAwkMYvWcOrDb4z%3DdDVmw14mzQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKjP07ngUQvLJfooY4RDBrZevdvdQ9-ir1sfEkvVcRHTinGyew%40mail.gmail.com.