My models.py is below:
from django.db import models
from extensions.models import SipExtension
xmpp_users = SipExtension.objects.values_list('real_name',flat=True)
xmpp_users_choices = [('', 'None')] + [(real_name,real_name) for real_name in xmpp_users]
class xmpp_buddy_groups(models.Model):
group_name = models.CharField(max_length=30)
name = models.CharField(choices=xmpp_users_choices,max_length=100)
def __str__(self):
return '%s %s' % (self.group_name,self.name)
my forms.py is below:
from django import forms
from .models import xmpp_buddy_groups
from extensions.models import SipExtension
class xmpp_buddy_groups_form(forms.ModelForm):
xmpp_users = SipExtension.objects.values_list('real_name',flat=True)
xmpp_users_choices = [('', 'None')] + [(real_name,real_name) for real_name in xmpp_users]
name = forms.ChoiceField(xmpp_users_choices,required=True, widget=forms.SelectMultiple())
# name = forms.ModelChoiceField(widget=forms.CheckboxSelectMultiple,queryset=SipExtension.objects.values_list('real_name',flat=True),required=True)
class Meta:
model = xmpp_buddy_groups
fields = ['group_name']
my views.py is below:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from .forms import xmpp_buddy_groups_form
@login_required
def index(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = xmpp_buddy_groups_form(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
#return HttpResponseRedirect('/index.html')
form.save()
# if a GET (or any other method) we'll create a blank form
else:
form = xmpp_buddy_groups_form()
print(form.errors)
return render(request, 'xmpp/index.html', {'form': form})
My template index.html is below
{% extends 'base.html' %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create Buddy Groups">
</form>
{% endblock %}
The django gives me invalid choices error. Image is attached.
Thanks to help me. It seems lime the choices for models and choices shown in templates through forms are not matching but I cannot figure out the reason since both are exactly same choices.
models.py
from django.db import models
from extensions.models import SipExtension
class xmpp_buddy_groups(models.Model):
group_name = models.CharField(max_length=30)
name = models.ForiegnKey(SipExtension)
def __str__(self):
return '%s %s' % (self.group_name,self.name)
forms.py
from django import forms
from .models import xmpp_buddy_groups
from extensions.models import SipExtension
class xmpp_buddy_groups_form(forms.ModelForm):
class Meta:
model = xmpp_buddy_groups
fields = ['group_name']
def __init__(self, *args, **kwargs):
super(xmpp_buddy_groups_form, self).__init__(*args, **kwargs)
if self.instance:
self.fields['name'].initial = SipExtension.objects.get(pk=self.instance.SipExtension).real_name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.