Create Model objects inside another models's Adminview
I'm trying to generalize access permissions to certain Nodes for each of my Groups. I have an Access Model model with three different fields: access_type, group and node. Each group can have either Source, Destination or Bidirectional Access to a node which has a name:
class Access(models.Model):
class AccessType(models.TextChoices):
BIDIRECTIONAL = "bi", "Bidirectional"
SOURCE = "src", "Source"
DESTINATION = "dst", "Destination"
access_type = models.CharField(max_length=3, choices=AccessType.choices)
group = models.ForeignKey(Group,on_delete=models.CASCADE, related_name='GroupAccess')
node = models.ForeignKey(DicomNode, on_delete=models.CASCADE)
name = models.CharField(unique=True, max_length=128, null = True)
Similar to the common permission view inside the adminView I'd like to create Access objects inside the GroupView, so that they are created with the group which I am inside at the moment. Also, I'd like them do be displayed on the left side with all available Accesstypes and nodes, seperated with an '|' just like all the available permissions:
I have done the same already with a form to add existing users to a group:
class GroupForm(forms.ModelForm):
users = forms.ModelMultipleChoiceField(
label='Users',
queryset=User.objects.all(),
required=False,
widget=admin.widgets.FilteredSelectMultiple(
"users", is_stacked=False))
class Meta:
model = Group
exclude = () # since Django 1.8 this is needed
widgets = {
'permissions': admin.widgets.FilteredSelectMultiple(
"permissions", is_stacked=False),
}
class MyGroupAdmin(GroupAdmin):
form = GroupForm
list_display = ["name", "pk"]
def save_model(self, request, obj, form, change):
# save first to obtain id
super(GroupAdmin, self).save_model(request, obj, form, change)
obj.user_set.clear()
for user in form.cleaned_data['users']:
obj.user_set.add(user)
def get_form(self, request, obj=None, **kwargs):
if obj:
self.form.base_fields['users'].initial = [o.pk for o in obj.user_set.all()]
else:
self.form.base_fields['users'].initial = []
return GroupForm
So my question is: how do I display every possible access configuration and then create this object with existing Nodes?
I'm very new to django and I honestly don't know where to start