0

i want to add if user is super user show companies and add company anyone can help with what should add in class to do it this is class code just showing in tepmalte page all comapany and add new company not working

views.py

class Companies(LoginRequiredMixin, FormView, ListView):
    """
    Company add edit delete view
    """
    model = Company
    template_name = 'company/index.html'
    form_class = AddCompanyForm
    success_url = reverse_lazy('companies:index')
    object_list = Company.objects.all()

    def form_valid(self, form):
        user, created = User.objects.get_or_create(username=form.cleaned_data['username'],
                                                   email=form.cleaned_data['email'])
        user.set_password(form.cleaned_data['password'])
        user.save()
        if created:
            address = Address(label=form.cleaned_data['label'], city=form.cleaned_data['city'],
                              area=form.cleaned_data['area'], long=form.cleaned_data['longitude'],
                              lat=form.cleaned_data['latitude'], country=form.cleaned_data['country'])
            address.save()

            company = Company(owner=user, name=form.cleaned_data['name'],
                              phone_number=form.cleaned_data['phone_number'], logo=form.cleaned_data['logo'],
                              address=address, water_source=form.cleaned_data['water_source'])
            company.save()
        return super().form_valid(form)

form.py

class AddCompanyForm(forms.ModelForm):
    """
    Add company model form
    """
    name = forms.CharField(required=True)
    password = forms.CharField(widget=forms.PasswordInput())
    logo = forms.ImageField(required=False)
    phone_number = forms.CharField(widget=forms.NumberInput())
    label = forms.CharField(max_length=20)
    country = forms.ModelChoiceField(queryset=Country.objects.all())
    city = forms.ModelChoiceField(queryset=City.objects.all())
    area = forms.ModelChoiceField(queryset=Area.objects.all())
    latitude = forms.CharField(max_length=50, required=False)
    longitude = forms.CharField(max_length=50, required=False)
    water_source = forms.ModelChoiceField(queryset=WaterSource.objects.all())

    class Meta:
        model = User
        fields = ['name', 'username', 'email', 'password']

    def __init__(self, *args, **kwargs):
        super(AddCompanyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.layout = Layout(Row(Column('name', css_class='form-group col-md-6 mb-0'),
                                 Column('username', css_class='form-group col-md-6 mb-0'), css_class='form-row'),
                             Row(Column('email', css_class='form-group col-md-6 mb-0'),
                                 Column('password', css_class='form-group col-md-6 mb-0'), css_class='form-row'),
                             Row(Column('phone_number', css_class='form-group col-md-6 mb-0'),
                                 Column('logo', css_class='form-group col-md-6 mb-0'), css_class='form-row'),
                             Row(Column('label', css_class='form-group col'), css_class='form-row'),
                             Row(Column('country', css_class='form-group col'),
                                 Column('city', css_class='form-group col'), Column('area', css_class='form-group col'),
                                 css_class='form-row'), Row(Column('latitude', css_class='form-group col'),
                                                            Column('longitude', css_class='form-group col'),
                                                            css_class='form-row'),
                             Row(Column('water_source', css_class='form-group col'), css_class='form-row'))
        self.helper.layout = self.layout

I am trying to put if request.user.is_superuser but it is not working.

haduki
  • 1,145
  • 5
  • 23
  • If you are creating a default user in the form then it's not a superuser. Besides that your model form is named AddCompanyForm but in the Meta it's says based on User model ? – BDurand Dec 25 '22 at 11:30

0 Answers0