Django admin groups permissions and access

孤者浪人 提交于 2020-12-15 06:42:15

问题


I'm searching for a way to customize the Django Administration to support permissions and data based on the user group.

For example, I've just created the Developers1, Developers2 groups.. now I've also created the Transaction model, with AdminModel to specify how to list data.

Transacton model:

class Transaction(models.Model):
income_period_choices = (('Weekly', 'Weekly'), ('Fortnightly',
                                                'Fortnightly'))
chp_reference = models.CharField(max_length=50, unique=True)
rent_effective_date = models.DateField(null=True, blank=True)
income_period = models.CharField(max_length=11,
                                choices=income_period_choices,
                                null=True,
                                blank=True)
property_market_rent = models.DecimalField(help_text='Weekly',
                                        max_digits=7,
                                        decimal_places=2,
                                        null=True,
                                        blank=True)
*group = models.ForeignKey(Group, on_delete=models.CASCADE)

im not sure about the *group field, should i delete it , or should i create Charfield, which is not a foreignkey to the django.contrib.auth.group model?

and this is the admin transaction:

 @admin.register(Transaction)
 class TransactionAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super().save_model(request, obj, form, change)

def get_queryset(self, request):
    qs = super().get_queryset(request)
    # for s in qs:
    if request.user.is_superuser:
        return qs
    return qs.filter(group_name__in=Group)

search_fields = ['chp_reference','familymember__name']
inlines = [FamilyGroupInline,FamilyMemberInline]

what im trying to do is i want each group to only access its own Transaction model, and each group can add, delete, update and view their own Transactions only(eg developers1 group cant access developers2 Transactions and vice versa)

any thoughts should be appreciated

thanks!:)


回答1:


To do that you can override the get_queryset method to only return the objects that are relevant to these users (based on their permissions, groups or any condition you want).



来源:https://stackoverflow.com/questions/64876166/django-admin-groups-permissions-and-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!