Re: dynamically excluding fields in admin form
You could do this with some javascript, which might work better in the following scenario: If you change the state of Completed between True and False, then other options will be enabled/disabled for you by the javascript My "take" on the admin screens is its a facility to fiddle with the model, and less a "workflow" oriented system. For "workflow" you would likely create your own views/templates/etc. So, even if you say "a stockorder can never be switched from completed to uncompleted", there are times you may require adjustments because something went awry. This is exactly what the admin screens are for: the admin fixing the data. If you want to prevent your users from doing that operation (completed to uncompleted), then write views that prevent this. If you determine that the model does not allow that transition, then you can do that enforcement inside the model (the clean() method in version 1.2), so if you were to make that "tweak" in the admin screen, it will refuse to save. On Feb 28, 5:00 am, Simon Davieswrote: > Hi > > I have a model which I access from the admin application. I want to > exclude some fields dynamically. > > My model looks like this: > > class StockOrder(models.Model): > number_of_items_pending_order = > models.PositiveIntegerField(max_length=5, default=1) > number_of_items_ordered = models.PositiveIntegerField(max_length=5, > default=1) > number_of_items_delivered = models.PositiveIntegerField(max_length=5, > default=1) > order_date = models.DateTimeField(auto_now_add=True, > verbose_name=u'Date ordered') > confirmed = models.BooleanField(default=False) > confirmed_date = models.DateTimeField(verbose_name=u'Date confirmed', > null=True, blank=True) > cancelled = models.BooleanField(default=False) > cancel_date = models.DateTimeField(verbose_name=u'Date cancelled', > null=True, blank=True) > completed = models.BooleanField(default=False) > completion_date = models.DateTimeField(verbose_name=u'Date > completion', null=True, blank=True) > > I want to exclude some fields depending on the state of others. So if > completed is True I don't want to see the confirmed or cancelled > flags. > > The options to exclude fields I understand are: > > 1: ModelAdmin.exclude attribute in admin.py. I can't see how this > can be manipulated dynamically however. > > 2. I create my own Modelform and set the form attribute in admin.py > to the modelform, the modelform will look something like this: > > class StockOrderForm(ModelForm): > class Meta: > model = StockOrder > exclude = ('cancelled',) > > However I run into the same problem as in 1, how can I manipulate the > exclude parameter. Is it possible to do so in __init__? > > Thanks > > Simon -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: dynamically excluding fields in admin form
On Feb 28, 12:21 pm, Simon Davieswrote: > figured out how to do it using the get_form method like this: > > class AccessoryStockOrderAdmin(admin.ModelAdmin): > form = AccessoryStockOrderForm > def get_form(self, request, obj=None, **kwargs): > form = super(AccessoryStockOrderAdmin, self).get_form(request, > obj, **kwargs) > if obj == None: > del form.base_fields['cancelled'] > return form > If it is just the add_view you want to change another way is to modify the add_view method: def add_view (self, *args, **kwargs): self.exclude = ('primary_phone','primary_email','primary_address') return super(ContactAdmin, self).add_view(*args, **kwargs) But I found doing this I needed to explicitly reset exclude to None for other views - as it was being remembered somewhere. -Preston > On 28 Feb, 13:00, Simon Davies wrote: > > > > > Hi > > > I have a model which I access from the admin application. I want to > > exclude some fields dynamically. > > > My model looks like this: > > > class StockOrder(models.Model): > > number_of_items_pending_order = > > models.PositiveIntegerField(max_length=5, default=1) > > number_of_items_ordered = models.PositiveIntegerField(max_length=5, > > default=1) > > number_of_items_delivered = > > models.PositiveIntegerField(max_length=5, > > default=1) > > order_date = models.DateTimeField(auto_now_add=True, > > verbose_name=u'Date ordered') > > confirmed = models.BooleanField(default=False) > > confirmed_date = models.DateTimeField(verbose_name=u'Date > > confirmed', > > null=True, blank=True) > > cancelled = models.BooleanField(default=False) > > cancel_date = models.DateTimeField(verbose_name=u'Date cancelled', > > null=True, blank=True) > > completed = models.BooleanField(default=False) > > completion_date = models.DateTimeField(verbose_name=u'Date > > completion', null=True, blank=True) > > > I want to exclude some fields depending on the state of others. So if > > completed is True I don't want to see the confirmed or cancelled > > flags. > > > The options to exclude fields I understand are: > > > 1: ModelAdmin.exclude attribute in admin.py. I can't see how this > > can be manipulated dynamically however. > > > 2. I create my own Modelform and set the form attribute in admin.py > > to the modelform, the modelform will look something like this: > > > class StockOrderForm(ModelForm): > > class Meta: > > model = StockOrder > > exclude = ('cancelled',) > > > However I run into the same problem as in 1, how can I manipulate the > > exclude parameter. Is it possible to do so in __init__? > > > Thanks > > > Simon -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: dynamically excluding fields in admin form
figured out how to do it using the get_form method like this: class AccessoryStockOrderAdmin(admin.ModelAdmin): form = AccessoryStockOrderForm def get_form(self, request, obj=None, **kwargs): form = super(AccessoryStockOrderAdmin, self).get_form(request, obj, **kwargs) if obj == None: del form.base_fields['cancelled'] return form On 28 Feb, 13:00, Simon Davieswrote: > Hi > > I have a model which I access from the admin application. I want to > exclude some fields dynamically. > > My model looks like this: > > class StockOrder(models.Model): > number_of_items_pending_order = > models.PositiveIntegerField(max_length=5, default=1) > number_of_items_ordered = models.PositiveIntegerField(max_length=5, > default=1) > number_of_items_delivered = models.PositiveIntegerField(max_length=5, > default=1) > order_date = models.DateTimeField(auto_now_add=True, > verbose_name=u'Date ordered') > confirmed = models.BooleanField(default=False) > confirmed_date = models.DateTimeField(verbose_name=u'Date confirmed', > null=True, blank=True) > cancelled = models.BooleanField(default=False) > cancel_date = models.DateTimeField(verbose_name=u'Date cancelled', > null=True, blank=True) > completed = models.BooleanField(default=False) > completion_date = models.DateTimeField(verbose_name=u'Date > completion', null=True, blank=True) > > I want to exclude some fields depending on the state of others. So if > completed is True I don't want to see the confirmed or cancelled > flags. > > The options to exclude fields I understand are: > > 1: ModelAdmin.exclude attribute in admin.py. I can't see how this > can be manipulated dynamically however. > > 2. I create my own Modelform and set the form attribute in admin.py > to the modelform, the modelform will look something like this: > > class StockOrderForm(ModelForm): > class Meta: > model = StockOrder > exclude = ('cancelled',) > > However I run into the same problem as in 1, how can I manipulate the > exclude parameter. Is it possible to do so in __init__? > > Thanks > > Simon -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
dynamically excluding fields in admin form
Hi I have a model which I access from the admin application. I want to exclude some fields dynamically. My model looks like this: class StockOrder(models.Model): number_of_items_pending_order = models.PositiveIntegerField(max_length=5, default=1) number_of_items_ordered = models.PositiveIntegerField(max_length=5, default=1) number_of_items_delivered = models.PositiveIntegerField(max_length=5, default=1) order_date = models.DateTimeField(auto_now_add=True, verbose_name=u'Date ordered') confirmed = models.BooleanField(default=False) confirmed_date = models.DateTimeField(verbose_name=u'Date confirmed', null=True, blank=True) cancelled = models.BooleanField(default=False) cancel_date = models.DateTimeField(verbose_name=u'Date cancelled', null=True, blank=True) completed = models.BooleanField(default=False) completion_date = models.DateTimeField(verbose_name=u'Date completion', null=True, blank=True) I want to exclude some fields depending on the state of others. So if completed is True I don't want to see the confirmed or cancelled flags. The options to exclude fields I understand are: 1: ModelAdmin.exclude attribute in admin.py. I can't see how this can be manipulated dynamically however. 2. I create my own Modelform and set the form attribute in admin.py to the modelform, the modelform will look something like this: class StockOrderForm(ModelForm): class Meta: model = StockOrder exclude = ('cancelled',) However I run into the same problem as in 1, how can I manipulate the exclude parameter. Is it possible to do so in __init__? Thanks Simon -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.