1

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.

OptimusPrime
  • 777
  • 16
  • 25

1 Answers1

0

Not sure if this is an answer, but.. Are you sure that your User class inherits from the PermissionsMixin?

class User(AbstractBaseUser, PermissionsMixin):
     pass
mirek
  • 1,140
  • 11
  • 10
  • Thanks, I am not sure if this would work, but I can remember that the issue was because my Django app wasn't using the integrated authentication. It was set up to use custom user accounts which is why it didn't work, but your answer might work as well. Unfortunately, this is so long ago that I don't know if I would be able to locate the project and code to try it out. – OptimusPrime Jun 01 '21 at 19:05
  • @mirek - I am facing same issue. having same problem as OptimusPrime facing. I am already inheriting permissionmixin. still no luck – trex Jul 09 '21 at 08:15