Hi ,
You can also try this.
vendor = User.objects.get(pk=request.user.id).vender
Thanks,
Indu Chouhan
On Wednesday, June 9, 2021 at 12:44:14 PM UTC+5:30 gwisum...@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 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.nameMy Viewdef 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
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4cf4e248-3d1a-48ac-85a4-53771ec8f56an%40googlegroups.com.
No comments:
Post a Comment