override Create.get_permission_required

i have custom user model as follows

class User(AbstractUser): 
    # if you need any other information regarding any type of these following kinds of users make a model for them
    USER_TYPE_CHOICES = (
    (1, 'admin'),
    (2, 'supervisor'),
    (3, 'instructor'),
    (4, 'student'),
    (5, 'customer'),
    (6, 'employee'),
    (7, 'advertiser'),
  )
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True,)
    is_active = models.BooleanField(default=True)
    is_superuser = models.BooleanField(default=False) #is_admin = models.BooleanField(default=False) 
    objects = newUserManager()
    USERNAME_FIELD = 'email' # string describing the name of the field on
    REQUIRED_FIELDS = ["username"] # list of the field names that will be prompted
    user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES, null=True, blank=True)
    class Meta:
        permissions = [
            ("change_post_status", "Can change the status of posts"),
            ("close_discussions", "Can remove a post by setting its status as closed"),
            ('can_publish', 'Can Publish Posts'),
        ]

and post model

class Post(models.Model):
    id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
    title = models.CharField(max_length=200, db_index=True, blank=False, default='')
    maintainer = models.ForeignKey(Maintainer, on_delete=models.SET_NULL, blank=True, null=True, related_name='post_maintainers')
    creator = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="post_creator")
    ...

and post maintainer model

class Maintainer(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='maintainers')
    class Meta:
        ordering = ['last_name', 'first_name'] 
    def get_absolute_url(self):
        return reverse('maintainer-detail', args=[str(self.id)])
    def __str__(self) -> str:
        return '{0}, {1}'.format(self.last_name, self.first_name)

and a model represent ManyTomany relation between users and posts

class PostInstance(models.Model): # Represenrt a Many To Many relation btn both post and users
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True, help_text="Unique ID for this particular post across whole blog")
    post = models.ForeignKey(Post, on_delete=models.RESTRICT, null=True)
    commenter = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name="commenter")
    class Meta:
           permissions = (("can_renew", "Set post new date"), ("can_edit", "Edit post details"))

the problem is when i try to go to create maintainer view

class MaintainerCreate(PermissionRequiredMixin, CreateView):
    permission_required = ['blog.can_renew', 'account.can_publish']
    model = Maintainer
    fields = ['first_name', 'last_name',]

i got this error

ImproperlyConfigured at /blog/maintainer/create/
MaintainerCreate is missing the permission_required attribute. Define MaintainerCreate.permission_required, or override MaintainerCreate.get_permission_required().

the problem is that i don’t have any groups in user admin pages to assign the user to it
although i already put the following line in admin.py
filter_horizontal = ('groups', 'user_permissions',)
do i have to do custom migration methods
or do i have to override these methods

    def get_permission_required(self):
        perms = super(MaintainerCreate, self).get_permission_required()
        return perms
    def has_permission(self):
        perms = super(MaintainerCreate, self).has_permission()
        return perms
    def get(self, request, *args, **kwargs):
        return HttpResponse("Create Blog Maintainer")

or what to do to show the groups in users admin page
i don’t know a straightforwoard method really confused
some help please brother @KenWhitesell

What’s wrong in the Question such no one reply me
i have checked my superuser that already been logged in to do all that kind of actions and it has all permissions already

>>> from account.models import User
>>> user = User.objects.get(username="abunagy")
>>> user.has_perm("blog.add_postinstance")
True
>>> user.has_perm("blog.can_renew_postinstance")
True
>>> user.has_perm("account.can_publish_user")
True

What to do pleas any help Bro @kenkennie ?

Even tel me that my question is wrong only Pleeeeeeease

It looks like your view class is correct.

Can you also show is your urls.py? The one that contains this specific view?

Also make sure that you restarted the server. If the error still persists. Please paste the entire traceback here as code.

In addition to @leandrodesouzadev 's fine answer, I’d also suggest you make 1000% sure that you don’t have a second version of MaintainerCreate defined in any view file.

There’s nothing wrong with your question - or the code I can see. So the issue is in something we can’t see. Now we just need to figure out what that is.

(Also, you need to remember that we here are all volunteers here and are helping people in our spare time. You shouldn’t expect a response in less than a day.)

1 Like

Thanks brothers @KenWhitesell and @leandrodesouzadev
My problem Now is How can I get groups in my user admin pages to assign a group to users
i can’t see any group field in admin users page
here is the admin user page

any suggestion please on how to show groups there?

Now the Error is gone but i see just a white page

here is my test maintainer_forn,html form

<div class="col-md-12">
    <div class="section-title">
        <h2><i class="fa fa-clock-o"></i> {% block head_title %} Create Maintainer{% endblock head_title %}</h2>
    </div>
</div>
<form action="" method="post">
    {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table>
    <input type="submit" value="Submit" />
</form>
{% endblock body %}

**


**
what can cause this form to fail render the fields
Or i have to create a function based view and form rather than a class better???
aside of that
i am sorry for calling you a lot but saw you replied on some other questions and thought that you have seen my question and bothered by something wrong so i have called again and said please tell me if the question is wrong itself

please the last part of how to show the group in custom user model admin page i would be so grateful to you if you told me anything

Since the Many-to-many relationship between Group and User is defined in Group, you need to create an InlineModelAdmin class to define that relationship from the perspective of User. (It’s similar to how you’re currently doing it for your profile class.)

And what does your “Create Blog Maintainer” view currently look like?

Really with that easy !! how awesome are you brother @KenWhitesell
i am really feel like i have a big problem in concentration as if i concentrated well i will never ask but
now the create view is what i have provided above

class MaintainerCreate(PermissionRequiredMixin, CreateView):
    permission_required = ['blog.can_renew', 'account.can_publish']
    model = Maintainer
    fields = ['first_name', 'last_name',]

i didn’t really use class based views, i have just tried to use is for simplisity rather than create a function
so i don’t know fro where is coming this line as i have just a
head title
Create Maintainer

You need to find it then, it must be somewhere in your project.

1 Like

i found the line really i bothered you a lot by trivial problems, don’t know how to thanks you bro for your great leniency

You tell me - does it work?

it was

    def get_permission_required(self):
        perms = super(MaintainerCreate, self).get_permission_required()
        return perms
    def has_permission(self):
        perms = super(MaintainerCreate, self).has_permission()
        return perms
    def get(self, request, *args, **kwargs):
        return HttpResponse("Create Blog Maintainer")

i was about to edit these functions and added them and forgot to remove them
i don’t know why i always forget a lot of messy codes and rename the copied functions to adapt other models really i mess a lot, but thanks so much my dear brother @KenWhitesell