5๐
โ
You can easily create a group by doing the following:
from django.contrib.auth.models import Group
newgroup = Group.objects.create(name=course.name)
You can put this code in your models like this (or maybe create a custom model manager):
from django.contrib.auth.models import User, Group
class Course(models.Model):
name = models.CharField(max_length=100)
@classmethod
def create(course, name):
newcourse = course(title=name)
# create the group for the course
newgroup = Group.objects.create(name=newcourse.name)
return newcourse
Then, you can create your course:
course = Course.create(name="Django: The Web framework for perfectionists with deadlines")
๐คDan Aronne
1๐
Daniel it seems a good approximation. I have managed a solution as well overriding the method save_model for the model CourseAdmin, inside the admin.py. Thanks a lot, your implementation seems cleaner!
Here is what I have done:
โโ admin.py โโ
from django.contrib.auth.models import Group
class CourseAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
g = Group(name=obj.code)
g.save()
obj.save() # Without this line the object is not saved into the Course model!
admin.site.register(CourseAdmin)
๐คediskrad
Source:stackexchange.com