I want to know how to deal with this task. here are the details: this is product_feature model
class Feature(models.Model):
has_value_choice = [
('y', 'Yes'),
('n', 'No'),
]
feature_id = models.AutoField(primary_key=True)
feature_name = models.CharField(max_length=255, null = False)
product_type = models.ForeignKey(Product_type, on_delete=models.SET_NULL, null=True)
feature_has_value = models.CharField(choices=has_value_choice, max_length = 1, null = False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
class Product_feature(models.Model):
product_feature_id = models.AutoField(primary_key=True)
feature = models.ForeignKey(Feature, on_delete=models.SET_NULL, null=True)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
product_feature_value = models.CharField(max_length=255, null = False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
here, I'm passing features data to the template to save features of the specific product in the product_feature model.
<form>
<table class="table table-bordered">
<thead>
<tr>
<th>Features</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for data in features_data %}
<tr>
{% if data.feature_has_value != 'n' %}
<td>{{ data.feature_name }}</td>
<td><input type="text" name="{{ data.feature_name }}" id="{{ data.feature_name }}" placeholder="Enter {{ data.feature_name }}" class="form-control" required/></td>
{% else %}
<td>{{ data.feature_name }}</td>
<td>
<select id="{{ data.feature_name }}" name="{{ data.feature_name }}" class="form-control" required>
<option selected id="select" value="yes">Yes</option>
<option value="no">No</option>
</select>
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</form>
Now, the thing is how to insert this form data to the product features table( product_feature model at the starting of the question).
I want to insert multiple records in the product features a table with each record have it's feature_id, product_id & product_feature value.
I'm really confused about how to do this I'm new in Django development.
You can use a django admin which defaults adds to your url In admin.py register models , you can use default ui
from django.contrib import admin
# Register your models here.
from medicine.models import Address,Shop
class ShopAdmin(admin.ModelAdmin):
list_display = ('name', 'phone', 'address')
class AddressAdmin(admin.ModelAdmin):
list_display = ('line_1', 'line_2', 'city', 'state', 'lat', 'lng')
admin.site.register(Shop, ShopAdmin)
admin.site.register(Address, AddressAdmin)
./manage.py make migrations ./manage.py createsuperuser and your app in insatlled apps
For cutsomized ui you need to have a another form that takes data and give it to backed using a request.body as json
The best practice is using django rest framework where you can define your end points and use seriizarers which gives the way to communicate in json/xml any supportable format
For inserting of data take everything as query param or payload body
For just creating objects to your model use this
obj = MyModel.objects.create(value)