-2

I have two models, Buyer and Merchant with the default user type after the account creation being Buyer.

# Abstract User
class User(AbstractUser):
    is_buyer = models.BooleanField(default=False)
    is_merchant = models.BooleanField(default=False)
    date_created = models.DateTimeField(default=timezone.now)


# Buyer
class Buyer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    pin = models.CharField(max_length=6, blank=True)
    items = models.ManyToManyField(Product, blank=True)

    # items = models.ManyToManyField(Product, blank=True)

    def __str__(self):
        return f'{self.user.username}'


# Merchant
class Merchant(models.Model):  # items
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    pgp = models.CharField(max_length=150, blank=True)
    # image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username}'

And I have a become-a-merchant.html template in which I would like to make the Buyer be able to become a Merchant after checking a checkbox and pressing a button.

{% extends 'base.html' %}

{% block content %}
<input type="checkbox" name="checkbox" value="check">
<label for="checkbox">Become a merchant.</label>
<br><br>
<button type="submit">Upgrade</button>
{% endblock %}

How can I do a form that upgrades my user type.

static-man
  • 29
  • 7

1 Answers1

0

You have submitted too little information on whether you are handling this with a function or a generic class. Also, I have a feeling that you don't need to do so many classes if you are using flags in user. If it's the generic class, you'll need to update the User class this way.

from django.views.generic.edit import UpdateView
from .models import User # or other location

# model
class UserUpdateView(UpdateView):
    model = User
    fields = ['is_buyer, is_merchant']

# template
{% extends 'base.html' %}
{% block content %}
<form method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update">
</form>
{% endblock %}

Also, check the docs.

ttt
  • 300
  • 1
  • 6
  • I was getting an Unknown field(s) (is_buyer, is_merchant) specified for User error. And then edited the fields to fields = ['is_buyer', 'is_merchant']. – static-man Jul 25 '22 at 19:24
  • It is not possible that the fields are unknown. You must have made a mistake somewhere. Make sure you correctly include `from .models import User # or other location`. If it still doesn't work, include the code and screenshots. – ttt Jul 26 '22 at 00:50
  • I'm getting the error: Generic detail view UserUpdateView must be called with either an object pk or a slug in the URLconf. – static-man Aug 24 '22 at 18:00
  • Show your `urls.py` – ttt Aug 31 '22 at 23:51