[Answer]-Django change foreignkey Field

1👍

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!

👤Johndt

Leave a comment