0

I have materials recorded in the database and belonging to different users. those are either private access, or are part of a group, or free access to all. I want, for example, a current user can have access to all the materials open access (but can not modify those he did not create (readonly fields). But, He could add other records ..

I have a model

DROITS = (
('lecture', 'all'),
('groupe', 'group')
('private', 'private data')

class Material(models.Model):
    name = models.CharField(_('name'), max_length=50)
    category = models.ForeignKey(Category, verbose_name=_('category'))
    user = models.ForeignKey(User, default=None, blank= True, null = True)
    droits_acces = models.CharField(_('access right'), max_length=150, choices =      DROITS, default= 'private' )
    groupe = models.ForeignKey(Group,  verbose_name=_('group'), blank = True, null= True, default = None)

Admin :

class MaterialAdmin(admin.ModelAdmin):
   list_display = ('name', 'description', 'user', 'created', 'droits_acces')
   inlines = (MediaInline, UniteProperty2Inline, Essai_TemperatureInline)
   def queryset(self, request):
   qs = super(MaterialAdmin, self).queryset(request)
   if request.user.is_superuser:
      return qs
   else:
.....

the first thing is to associate user and material. how can we do? with the class Material, I filter only the fields related material

after if the material does not belong to the current user I have to put 'Essai_TemperatureInline' read access only

user3172700
  • 107
  • 2
  • 13

1 Answers1

0

I'm not sure what you are asking for, but I think you are asking how to know if this Material belongs to this User.

That would be something like this:

belongToMe = Materials.objects.filter(user=u).filter(droits_acces="lecture")

This returns all Materials that belongs to user u and have lecture in droits_acces.

Maybe you are asking how to know if not belongs, that may be something like this:

notBelongToMe = Materials.objects.exclude(user=u).filter(droits_acces="lecture")

Here I get all that doesn't belong to user and have lecture in droits_acces

Hope it helps.

Sascuash
  • 3,661
  • 10
  • 46
  • 65
  • for all Materials in acces="lecture" and not belong to the user, I prefer to write : qs.filter(~Q(user=request.user) & Q(droits_acces= 'lecture')). Now, How to do put Essai_TemperatureInline with readonly fields option for these materials ? – user3172700 Apr 01 '14 at 09:27