Monday, March 30, 2015

Re: How to prevent save/delete in the Admin

I am moved to say thank you to all the devs from day 1 for Django.

It is a marvellous piece of work.

All of it.

Thanks

Mike

On 30/03/2015 6:53 PM, Mike Dewhirst wrote:
> On 30/03/2015 10:48 AM, Mike Dewhirst wrote:
>> On 30/03/2015 3:11 AM, Melvyn Sopacua wrote:
>>> On Sunday 29 March 2015 22:04:23 Mike Dewhirst wrote:
>
> snip
>
>>>
>>> That said, maybe I'm not getting your workflow correctly, but to me it
>>> seems that if request.user is not owner, some fields should be readonly.
>>> And this is what ModelAdmin.get_readonly_fields() is for.
>>
>> Working on this as we speak!
>>
>>>
>>> If this doesn't work for you, can you explain why?
>>
>> I think it will work!
>>
>> More later.
>
>
> It works beautifully!
>
> # in utils.py
> def is_public_domain(self, request, obj=None):
> """Return readonly fields. That is the 'modified' fields or all
> fields depending on whether the substance is (a) yours or (b) not
> yours but the substance is in the public domain. self is the inline
> model and obj is the "master" model.
> """
> if obj is not None:
> if obj.company == get_user_company(request.user):
> return self.ro_fields
> else:
> return self.model._meta.get_all_field_names()
>
> # in substance admin.py
> from utils import is_public_domain
>
> class SubstanceAdmin(admin.ModelAdmin):
>
> def get_queryset(self, request):
> """ Limit the substances visible to just those owned by the
> company of which the user is a member or those in the public
> domain.
> """
> qs = super(SubstanceAdmin, self).get_queryset(request)
> return qs.filter(
> Q(access_status=PUBLIC_DOMAIN,)
> | Q(company=get_user_company(request),)
> )
> ...
> ro_fields = ['modified', 'modified_by',]
> get_readonly_fields = is_public_domain
> ...
> class SynonymsInline(admin.StackedInline):
> model = Synonym
> fk_name = 'substance'
> ro_fields = ['modified', 'modified_by',]
> get_readonly_fields = is_public_domain
> ...
>
>
> Thank you very much Melvyn.
>
> Cheers
>
> Mike
>
>>
>> Thank you Melvyn
>>
>> Mike
>>
>>>
>>
>
>


--

Climate Pty Ltd
PO Box 308
Mount Eliza
Vic 3930
Australia +61

T: 03 9787 6598
M: 0411 704 143


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5519D7E7.5020503%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment