Reputation: 427
this is my models.py
class Equipment(models.Model):#table
equipment_Number = models.CharField(max_length=50,default=0,unique=True) #column
equipment_Name = models.CharField(blank=True, max_length=50,default='Dry')
mfd = models.DateField( default=datetime.date.today) #column
make = models.CharField(max_length=200)
model = models.CharField(max_length=200)
location = models.CharField(max_length=200)
doors = models.BooleanField(default=True)
locks = models.BooleanField(default=False)
def __unicode__(self):
return self.Make
class Inspection(models.Model):
inspected = models.ForeignKey(Equipment)#the key that links the tables
does_It_Work = models.BooleanField(default=True)
repairs = models.CharField(max_length=5000)
ins_date = models.DateTimeField( default=datetime.datetime.now)
this is my form.py
class EquipmentForm(ModelForm):
class Meta:
model = Equipment
class InspectionForm(ModelForm):
class Meta:
model = Inspection
sorry, let me be more clear. with my code, the 'Inspection' class links to the 'Equipment' class by the 'make' property. I need it to link by the 'equipment_Number' property.
i changed the name of the equipment class to car as i thought it would make the concept more tangible and would make is easier for you all to help. turns out i just caused more confusion. thanks for all of your help. SO Rocks!
Upvotes: 0
Views: 1545
Reputation: 4327
If you want a form for your Equipment
model which displays only the equipment_Number
field, then you would define it as follows in your forms.py
:
class EquipmentNumberForm(ModelForm):
class Meta:
model = Equipment
fields = ['equipment_Number']
Saving this form (with it's POST data of course!) would create an instance of the Equipment
model with the specified equipment_Number
, and all other fields set to their default, or null value.
Note here that I set the fields
attribute of the Meta
class for the form. This is a list of strings which specifies which fields should be displayed by the form. You can, although this is not recommended, instead use the attribute exclude
, which will show all fields except for the ones specified in your list.
You can view the documentation here.
However, your question doesn't really make it clear if this is what you want.
As an aside, you should be careful with your naming conventions. Class names should always be CamelCase
and attribute, variable, and method names should always be lower_case_separated_by_underscores
. This will make your code much clearer to everyone who reads it, including yourself!
Upvotes: 1