Answer the question
In order to leave comments, you need to log in
How to limit visibility of model instance fields in Django?
Good afternoon.
A rather unusual task for Django, it seems to me.
Have a User model and allow a Supply data model
class Supply(model.Model):
name = models.CharField(...)
field1 = models.CharField(...)
field2 = models.CharField(...)
field3 = models.CharField(...)
class IntermediateTable(models.Model):
user = models.ForeignKey(User)
supply = models.ForeignKey(Supply)
access = models.ManyToManyField(Choices, null=True, blank=True)
class Choices(models.Model):
description = models.CharField(max_length=300)
Answer the question
In order to leave comments, you need to log in
Можно попробовать в admin.py переопределить метод get_form и при инициализации формы сделать там проверку на юзера, например:
class SupplyAdmin(admin.ModelAdmin):
available_fields = ('name', 'field1', 'field2')
hidden_fields = ('field3',)
def get_form(self, request, obj, **kwargs):
if request.user.username == '[email protected]':
self.fields = self.available_fields + self.hidden_fields
else:
self.fields = self.available_fields
return super(SupplyAdmin, self).get_form(request, obj, **kwargs)
Можно так попробовать:
class Choices(models.Model):
description = models.CharField(max_length=300)
users = models.ManyToManyField(User)
def some_view(request):
list = [item for item in Choices.objects.all() if request.user in item.users.all() ]
return render(request, 'template.html', locals() )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question