How to get one field from model in django How to get one field from model in django django django

How to get one field from model in django


Generally, When filter or get, you have to put query inside it, like

subscription_id = Text.objects.filter(fieldname="searchterm")

This will return a queryset.So to view this

subscription_id.values() #returns a list of objects(dicts)

If you want to get only subsID

subscription_id.values("subsID")

This also return you list which contains

[{"subsID":"value"}, {"subsID":"value"} ....]

If you want to get only values

subscription_id.values_list("subsID", flat=True)

This will return like

["value", "value", ....]


You have to equal subsID to the value you want to find.

subscription_id = Text.objects.filter(subsID=<your subscrition id variable>)

pay attention this will return a list []

subscription_id = Text.objects.get(subsID=<your subscrition id variable>)

This will return an object


Link

You can't use the model in the view, you need to use the ModelForm or Form.Once you use that one, you can specify which field is active or not or simply setting the attribute in the ModelForm,

exclude=['paramiter_name']

and it's done.

Good luck.