Y
Y
Yzurgzd2020-11-04 18:14:25
Django
Yzurgzd, 2020-11-04 18:14:25

How to merge duplicates in Django?

models

class Characteristics(models.Model):
    name = models.CharField('Наименование', max_length=50)
    slug = models.SlugField(max_length=160, unique=True)

class ProductCharacteristics(models.Model):
    product = models.ForeignKey(
        Product, verbose_name='Товар', on_delete=models.CASCADE)
    characteristic = models.ForeignKey(
        Characteristics, verbose_name='Характеристика', on_delete=models.CASCADE)
    value = models.CharField('Значение', max_length=50)


views
def get_characteristics(self):
        return ProductCharacteristics.objects.all()


sample
{% for characteristic in view.get_characteristics  %}
          <div class="border-bottom pb-4 mb-4">
            <h4>{{ characteristic.characteristic.name }}</h4>
            <!-- Checkboxes -->
            <div
              class="form-group d-flex align-items-center justify-content-between font-size-1 text-lh-lg text-body mb-1">
              <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="genderMen">
                <label class="custom-control-label text-lh-lg" for="genderMen">{{ characteristic.value }}</label>
              </div>
              <small>73</small>
            </div>
            <!-- End Checkboxes -->
          </div>
          {% endfor %}


Displayed like this
5fa2c583489bc010251131.png

How to determine if the name in Characteristics is the same, then the value value was written under the Brand in this case. And if the values ​​are the same, then it should not be repeated?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JRazor, 2020-11-04
@Yzurgzd

The name field is not unique. There can be any number of instances. Merge them - that's still crap, it's easier to immediately score there unique=True

class Characteristics(models.Model):
    name = models.CharField('Наименование', max_length=50, unique=True)

This is first.
Second - why get these values ​​from the product? It is necessary to call Characteristics and call on feedback. If it's a list of Characteristics - why print ProductCharacteristics at all. Most likely - two products, that's why the problem
And thirdly: the names of the models are only in the singular

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question