I really want to be able to use the django-autocomplete-light to enable users to create new Franchise names when they use the autocomplete box. I have set this up, but I don't get the option to create. I can see in the documentation about adding permissions, but I don't quite understand where it is done and also if I run in the shell, I get errors as below:
>>> from user.models import User
>>> user = User.objects.get(username="joeuser")
>>> permission = Permission.objects.get(name='Can add franchise')
>>> user.user_permissions.add(permission
>>> user.user_permissions.add(permission)
AttributeError: 'User' object has no attribute 'user_permissions'
The form which uses it looks as below:
franchisename = forms.ModelChoiceField(queryset=allfranchises,widget=autocomplete.ModelSelect2(url='/seasons/franchisesautocomplete/'),required=True)
The url looks like:
url(r'^franchisesautocomplete/', views.FranchiseAutocomplete.as_view(create_field='name'), name='franchiseautocomplete'),
The view looks like:
class FranchiseAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self, *args, **kwargs):
user = getuser(self.request)
if user == None:
#Set a blank queryset if the user is not logged in
qs = Franchise.objects.none()
else:
qs = Franchise.objects.all()
return qs
The autcomplete works in terms of returning the result set to the choice box, just can't get the create to work. Please can someone help.