I have set up a very basic Django website and I am looking to create a moderator group. I am unsure how to go about creating this programatically. Mainly, I am worried about which file I would need to put it in, and whether or not when I create it, will it show up on my admin site as a group. Otherwise, how else can I confirm it is made? Can I create groups from any app? For example, I have an app called 'users' which doesn't contain any models. Can I simply create my moderator group with example code for a generic group below in views.py or will that not work?
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from api.models import Project
new_group, created = Group.objects.get_or_create(name='new_group')
ct = ContentType.objects.get_for_model(Project)
permission = Permission.objects.create(codename='can_add_project',
name='Can add project',
content_type=ct)
new_group.permissions.add(permission)
Thanks for any help in advance!
0001_initial.py
# Generated by Django 3.2.6 on 2021-09-15 10:13
from django.db import models, migrations
def apply_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
Group.objects.bulk_create([
Group(name=u'group1'),
Group(name=u'group2'),
Group(name=u'group3'),
])
def revert_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
Group.objects.filter(
name__in=[
u'group1',
u'group2',
u'group3',
]
).delete()
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.RunPython(apply_migration, revert_migration)
]
Related
So basically i created a new Django project and an app which contains my model.
I have already created a free cluster on mongoDb atlas and connected my project via Djongo engine.
Issues:
when i first made make migrations and then migrated my model , it created a new database from scratch since i had not created it.
however my model name is Mtsdata and would like to create a corresponding collection. why is the collection not created though it shows in the migration files that i have created the model and also in my database , my django_migrations is updated ?
from django.db import models
class Mtsdata(models.Model):
Company = models.CharField(max_length= 32 , null=True, blank=True)
plant = models.CharField(max_length= 32 , null=True, blank=True)
class Meta:
managed = False
db_table = "Mtsdata"
urls.py
from django.contrib import admin
from django.urls import path
from interndata import views
urlpatterns = [
path('admin/', admin.site.urls),
path('mtsdata/',views.Mtsdatalist.as_view()),
]
views.py
from django.shortcuts import render
from . models import Mtsdata
from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, ListAPIView
# Create your views here.
class Mtsdatalist(ListAPIView):
def get(self,request):
queryset = Mtsdata.objects.all()
return Response(queryset.values())
My goal is to have one dummyuser in the database. I already wrote the following function
def get_test_user():
user, created = get_user_model.objects.get_or_create(username=TESTUSER_USERNAME)
user.set_password(TESTUSER_PASSWORD)
user.save
But where and how should I call it to achieve my goal. I am aware that testing django will create its own database and that there are functions for creating test users. But this should actually run in the production database as well.
You can create data migration, for example:
from django.contrib.auth.hashers import make_password
from django.db import migrations
def create_user(apps, schema_editor):
User = apps.get_registered_model('auth', 'User')
user = User(
username='user',
email='user#mail.com',
password=make_password('pass'),
is_superuser=False,
is_staff=False
)
user.save()
class Migration(migrations.Migration):
dependencies = [
('auth', '0001_initial')
]
operations = [
migrations.RunPython(create_user),
]
After seeing this post, I tried to create my own group at project setup with this migration :
from django.db import migrations
from django.contrib.auth.models import Group, Permission
def create_group(apps, schema_editor):
group, created = Group.objects.get_or_create(name='thing_managers')
if created:
add_thing = Permission.objects.get(codename='add_thing')
group.permissions.add(add_thing)
group.save()
class Migration(migrations.Migration):
dependencies = [
('main', '0002_auto_20160720_1809'),
]
operations = [
migrations.RunPython(create_group),
]
But I got the following error :
django.contrib.auth.models.DoesNotExist: Permission matching query does not exist.
Here is my model :
class Thing(models.Model):
pass
Why can't I do that? How could I solve this?
I use django 1.9.
Permissions are created in a post_migrate signal. They don't exist the first time migrations are run after a new model is added. It is probably easiest to run the post_migrate signal handler manually:
from django.contrib.auth.management import create_permissions
def create_group(apps, schema_editor):
for app_config in apps.get_app_configs():
create_permissions(app_config, apps=apps, verbosity=0)
group, created = Group.objects.get_or_create(name='thing_managers')
if created:
add_thing = Permission.objects.get(codename='add_thing')
group.permissions.add(add_thing)
group.save()
create_permissions checks for existing permissions, so this won't create any duplicates.
From this Django ticket, here's what worked for me in Django 3.0.4 and apparently will work in >=1.9:
from django.core.management.sql import emit_post_migrate_signal
def create_group(apps, schema_editor):
# Ensure permissions and content types have been created.
db_alias = schema_editor.connection.alias
emit_post_migrate_signal(2, False, db_alias)
# Now the content types and permissions should exist
Permission = apps.get_model('auth', 'Permission')
...
One solution is call the update_permissions command before try to append a permission
from django.core.management import call_command
def update_permissions(schema, group):
call_command('update_permissions')
operations = [
migrations.RunPython(update_permissions, reverse_code=migrations.RunPython.noop),
migrations.RunPython(create_group),
]
And as was commented don't import Group and Permission models use:
Group = apps.get_model("auth","Group")
Permission = apps.get_model("auth","Permission")
I try to create a view, that will accept POST requests and create new instances of my model(see bottom of post). I follow this tutorial. The problem is that when I access URL associated with view, which inherits from CreateAPIView I dont see a form in html representation of API for creation of new instances and I also see that it accepts GET requests, not POST as it mentioned in documentation.
Page looks like this
My views.py
from django.shortcuts import render
from rest_framework.generics import ListAPIView, CreateAPIView
from datingapp.models import Profile
from .serializers import ProfileSerializer, ProfileCreateSerializer
class ProfilesAPIView(ListAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
class ProfileCreateAPIView(CreateAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileCreateSerializer
My urls.py
from django.conf.urls import url
from django.contrib import admin
from datingapp.views import ProfilesAPIView, ProfileCreateAPIView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
]
My serializers.py
from rest_framework.serializers import ModelSerializer
from datingapp.models import Profile
class ProfileSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
class ProfileCreateSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
In my settings.py I have crispy_forms installed.
What am I doing wrong ?
UPD: here is what I want to achieve
As you see there is a form and it accepts only POST and also says that GET is not allowed
The problem is in your router. The first pattern matches both api/profiles/ and api/profiles/create/ so the second one will never be evaluated. You are seeing the ProfilesAPIView instead of the create view.
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
To fix it, either swap the order of the urls, or add a $ to the end of the first pattern. r'api/profiles/$'
I was following a tutorial and had a similar problem. Probably I was not following the same version of Django Rest Framework and they had changes.
But I solved this problem doing this.
class AssetBundleList(generics.ListAPIView):
to
class AssetBundleList(generics.ListCreateAPIView):
Hope this helps someone.
I was trying to create a custom permission in a migration, however after running migrate, the permission was not created in the permission table. Could someone point out what the error was?
Also I am not sure what I should use as the related model for ContentType as the permission is used for restricting users that can view a page which shows summary of users on the site.
Any help will be greatly appreciated, thanks.
def add_view_aggregated_data_permissions(apps, schema_editor):
ContentType = apps.get_model('django', 'ContentType')
Permission = apps.get_model('auth', 'Permission')
content_type = ContentType.objects.get(app_label='auth', model='user')
permission = Permission.objects.create(codename='can_view_data',
name='Can view data',
content_type=content_type)
I would recommend you to use the standard way to use custom permissions as described in the Django documentation. You will avoid many issues altogether.
To create custom permissions for a given model object, use the permissions model Meta attribute.
This example model creates a custom permission:
class MyModel(models.Model):
...
class Meta:
permissions = (
('view_data', "Can see available data"),
)
The only thing this does is create those extra permissions when you run manage.py migrate. Your code is in charge of checking the value of these permissions when a user is trying to access the functionality provided by the application...
Then you can use the permission_required decorator with your view to check for the specific permission:
from django.contrib.auth.decorators import permission_required
#permission_required('myapp.view_data')
def my_view(request):
...
I wanted to created a custom permission (read) for all app models. I did this two steps:
Create an extended permission from DjangoModelPermissions:
class DjangoModelPermissionsExtended(DjangoModelPermissions):
"""
"""
perms_map = {
'GET': ['%(app_label)s.read_%(model_name)s'],
'OPTIONS': [],
'HEAD': [],
'POST': ['%(app_label)s.add_%(model_name)s'],
'PUT': ['%(app_label)s.change_%(model_name)s'],
'PATCH': ['%(app_label)s.change_%(model_name)s'],
'DELETE': ['%(app_label)s.delete_%(model_name)s'],
}
Put it in each view I want to have read permission:
class ExampleViewSet(viewsets.ModelViewSet):
permission_classes = (
DjangoModelPermissionsExtended,
)
Create a django command customread.py:
from django.core.management.base import BaseCommand, CommandError
from project.app import models as app_models
from django.db import models
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
import inspect
class Command(BaseCommand):
help = 'Create the read permission to app models'
def handle(self, *args, **options):
for name, obj in inspect.getmembers(app_models):
if inspect.isclass(obj) and issubclass(obj, models.Model):
try:
self.add_canread(obj)
self.stdout.write(self.style.SUCCESS(
'created permission for %s' % obj
))
except Exception as e:
self.stdout.write(self.style.ERROR(
'Permission already exists for %s' % obj
))
def add_canread(self, object_class):
"""This a function that can be executed in order to create
new permissions (read view) to a class in DB.
"""
if inspect.isclass(object_class):
content_type = ContentType.objects.get_for_model(object_class)
permission = Permission.objects.create(
codename='read_{}'.format(object_class._meta.model_name),
name='Can view {}'.format(object_class.__name__),
content_type=content_type,
)
else:
msg = "The object is not a class"
print(msg)
Execute it after doing migrations:
python manage.py customread
As of django 1.8 and built-in migrations this is very painless.
All you need to do is add the permissions you want to the relevant
model
Run makemigration
./manage.py makemigrations
run the migration created in the step above
./manage.py migrate