0

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.

2 Answers 2

0

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)
Sign up to request clarification or add additional context in comments.

5 Comments

No! but I'm not using the Django default admin panel I have created my customized admin panel is there a solution to my question?
yes, you can read the value submitted to backend as dictionary and create object to your model
Please give any link if there is?
you need to have views fetch and validate your form and save! please mark this as answer, if it answers your question
0

I think what you're looking for is a ModelFormSet of feature forms. This allows you to duplicate one form for multiple objects. The documentation is a good on-ramp: https://docs.djangoproject.com/en/3.0/topics/forms/formsets/

2 Comments

thanks for this but you can check my question I work with normal or custom form not working with Django form so as per you there is another solution?
You can still use custom HTML with a Django Form or FormSet class used server side. This is covered in the Form documentaion here: docs.djangoproject.com/en/3.0/topics/forms/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.