1

I just follow the tutorial and got stuck when I saw form field not use <input> tag.

here my code

model.py

class Employee(models.Model):
companyid = models.CharField(max_length=100)
name = models.CharField(max_length=100)

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("system:detail",kwargs={'pk':self.pk})

here my system/templates/system/emp_form.html

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" class="btn btn-primary" value="Submit">
</form>

and here my view.py

class EmployeeCreateView(CreateView):
fields = ('nik','name')
model = models.Employee

def form_valid(self, form):
self.object = form.save(commit=False)
empcount = models.TempEmp.objects.filter(status=0).count()
self.object.companyid = CID + str(datetime.date().now()) + str(empcount+1)
self.object.save()
return super(ModelFormMixin, self).form_valid(form)

problem 1.

emp_form.html use {{ form.as_p }} that show all the form, I want to hide specific field like in my case is companyid that I need to computer generated.

problem 2.

can I use + for str on CID + str(datetime.date().now()) + str(empcount+1)

or any other solution maybe?

Thank you!

2
  • If you don't want the companyid in template use exclude to hide. Commented Apr 21, 2018 at 1:15
  • @RajaSimon can you give m some example?... Commented Apr 21, 2018 at 1:21

2 Answers 2

1

You can use ModelForm exclude to hide the form being render

class EmployeeCreateView(CreateView):
    fields = ['nik','name']
    exclude = ['companyid']
Sign up to request clarification or add additional context in comments.

Comments

1

OR, in the model itself, you can set editable to false. This makes it so it doesn't show up in any forms.

class Employee(model.Models):
     companyid = models.IntegerField(editable=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.