Handle a ModelMultipleChoiceField from the views.py

1

I have this field in my form

And I want the views.py to collect all the coins chosen, currently only returns the last one.

views.py

class userPageView(TemplateView):
template_name = 'user.html'

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['edit_profile_form'] = EditProfileForm(prefix='edit')
    return context

def post(self, request, *args, **kwargs):
    edit_name = request.POST.get('edit-name')
    edit_two_factors_auth = request.POST.get('edit-two_factors_auth')
    edit_coins = request.POST.get('edit-coins')
    if request.method == "POST": 
        if 'profileButton' in request.POST:
            if edit_name and (edit_name != request.user.name):
                request.user.name = edit_name
                request.user.save()
            print(edit_coins)
    return render(request, 'user.html')

The print () shows me only Ethereum.

The user model:

class Usuario(AbstractUser):
    name = models.CharField(max_length=12, help_text="The name must be between 2 and 12 characters")
    email = models.EmailField(max_length=60, unique=True, help_text="The email must be between 5 and 30 characters")
    password = models.CharField(max_length=78)
    change_password_code = models.CharField(blank=True,max_length=15)
    activated = models.BooleanField(default=False)
    activated_code = models.CharField(default="",max_length=15)
    ip = models.CharField(blank=True,max_length=15)
    last_login = models.DateField(default=now)
    wallets = models.ManyToManyField(Wallet)
    coins = models.ManyToManyField(Coin)
    avatar = models.CharField(blank=True,default="bitcoin.png",max_length=15)
    delete_code = models.CharField(default="",max_length=9,blank=True)
    two_factors_auth = models.BooleanField(default=False)
    two_factors_auth_code = models.CharField(default="",max_length=12,blank=True)
    fingerprint = models.CharField(max_length=64,blank=True)

How do I pick up the chosen values and store them in the user's coins field?

    
asked by XBoss 07.07.2018 в 13:09
source

1 answer

0

As easy as:

edit_coins = request.POST.getlist('edit-coins')
    
answered by 08.07.2018 / 12:49
source