-1

I have two users a customer and an admin. And I have an update and a create form with a status field and other fields. The status filed displays whether the order is received, scheduled, or in manufacturing. I want only the admin to update that field and customer to only view it. Can anyone tell me how can I implement it? Thanks!

dummy
  • 21
  • 6
  • 1
    you need to share codes regarding how you are maintaining these roles – ruddra Jun 17 '20 at 06:12
  • @ruddra you can have.look at it now – dummy Jun 17 '20 at 06:19
  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour), read about what's on-topic in the [help center](https://stackoverflow.com/help/on-topic), and ___read [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask)___. – Ivo Mori Jun 17 '20 at 08:20

1 Answers1

1

Maybe you can try like this by overriding OrderForm:

class OrderForm(ModelForm):
    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(OrderForm, self).__init__(*args, **kwargs)
        if not user.is_superuser:  # or do admin check
            self.fields['status_choices'].widget.attrs['readonly'] = True

    class Meta:
        model = Order
        fields = '__all__'
        exclude = ['user','date']

Then pass the user instance through the OrderForm when initiating it in the view:

form = OrderForm(request.POST or None, request.FILES or None,initial=initial_date, user=user)

Update

It might not work for intitial data because it has a choice which does not exist in the choices in the model field of Order model. Change it to:

initial_date = {
    'status':"Processing/Manufacturing"
}

You can also add it as default value in the model field as well:

  status = models.CharField(max_length = 100, choices = status_choices, default="Processing/Manufacturing")

If you do not want to display to field at all, then you can make it hidden with:

if not user.is_superuser:  # or do admin check
    self.fields['status_choices'].widget.attrs['hidden'] = True

Or just remove it:

if not user.is_superuser:  # or do admin check
    self.fields.pop('status_choices')
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • it's partially working, in the status drop-down filed it's still saying the field is required – dummy Jun 17 '20 at 06:52
  • it's working fine thanks a lot. May I know what if i want to show status field only when we are logged in as admin in the form , but not if we are logged in as customer. How can we implement that ? – dummy Jun 17 '20 at 07:17
  • NoneType' object has no attribute 'is_superuser' this is the error I'm getting now – dummy Jun 17 '20 at 07:24
  • because you are not passing `user` through the `OrderForm()` in the view. Pass it like this: `OrderForm(request.POST or None, ..., user=request.user)`. – ruddra Jun 17 '20 at 07:26
  • working fine , but now i cant update an order. for the update order it's giving me 'NoneType' object has no attribute 'is_superuser' error – dummy Jun 17 '20 at 07:32
  • please see my last comment. :) – ruddra Jun 17 '20 at 07:37
  • is there any way in which u can pop or hide more than one field ? – dummy Jun 17 '20 at 09:10
  • just add another line like status_choice with new field for pop or hide – ruddra Jun 17 '20 at 09:38
  • yeah i tried it's working , but i dont know how to hide the button as well – dummy Jun 17 '20 at 09:47
  • just use a `{% if user.is_superuser %}{% endif %}` – ruddra Jun 17 '20 at 09:51
  • alright lastly what if i want to get rid of textfield border outline ? – dummy Jun 17 '20 at 09:56
  • use CSS :) I can't help you with that. But if you write custom css classes and want to add it to a form field, then check this SO answer: https://stackoverflow.com/a/5827669/2696165 – ruddra Jun 17 '20 at 09:58