If you have the code in a file as you have then what's happening is that 
you trying to create the same Permission everytime Django runs.  What you 
want to do is to create the permission against the BlogPost model itself 
within the class Meta. Then run a makemigrations and migrate to create the 
permission once. After that you'll be able to use it.

See 
https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#custom-permissions

On Wednesday, 6 October 2021 at 04:38:14 UTC+1 aadil1...@gmail.com wrote:

> I am creating custom permission in my Django myapp's module with the 
> follwing code;
>
> from myapp.models import BlogPost
> from django.contrib.auth.models import Permission from 
> django.contrib.contenttypes.models import ContentType 
>
> content_type = ContentType.objects.get_for_model(BlogPost) 
>
> permission = Permission.objects.create(
> codename='can_publish', 
>   name='Can Publish Posts', 
>   content_type=content_type, )
>
> when i use this permission in my views, i get the following error,
>
> File 
> "/home/aadil/.local/share/virtualenvs/crawling_next-ao7NwWLu/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py",
>  
> line 423, in execute
>
> return Database.Cursor.execute(self, query, params)
>
> django.db.utils.IntegrityError: UNIQUE constraint failed: 
> auth_permission.content_type_id, auth_permission.codename
>
> I am new to djagno please help??
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1e303e72-1d0c-43b3-a23f-60bd0aa797c9n%40googlegroups.com.

Reply via email to