I have a problem and I haven't been capable for finding the solution. I've got the next class
#models.py
from django import models
class Suggest(models.Model):
user = models.ForeignKey(User, null= True, blank=True)
article = models.ForeignKey(Article)
name = models.ChardField(max_length=100)
types = ( ('suggest','Sugest'),('note','Note'),('complain','Complain'))
type = models.CharField(max_length=80, choices = types, default='suggest')
content = models.TextField()
dataX = models.CharField(max_length=200) #example
#forms.py
from django import form
from .models import Suggest
class SuggestForm(form.ModelForm): #File forms.py,
class Meta:
fields = ('name','type','content')
I've got some more complete but with this code is enough for explaining my doubts. I need create this form with two parameter (article, request.user), the article must keep associated through a hidden field. When the request user is logged in, in name must be equal to (request.user.first_name+" "+request.user.last_name) and save in user the id of the logged in user. When the user is logged in must show just content.
My second doubt is how I complete for instance dataX if this field isn't in the form
You can set user by creating an empty instance of the
Suggest
model:To display only part of the fields You can use two separate forms (with
name
and without) or add an{% if user.is_authenticated %}
statement in your template to skip thename
field when rendering HTML of the form.