A
A
Anna2018-02-14 19:24:41
Django
Anna, 2018-02-14 19:24:41

How to import from *.xlsx to a model?

There are models

# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=200, verbose_name="Категория")

    class Meta:
        ordering = ["name"]
        verbose_name = "Категория"
        verbose_name_plural = "Категории"

    def __str__(self):
        return self.name

    '''def get_update_url(self):
        return reverse('category_update', args=[str(self.id)])

    def get_delete_url(self):
        return reverse('category_delete', args=[str(self.id)])'''

def default_vendor_code():
    return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))

class Product(models.Model):
    title = models.CharField(max_length=200, verbose_name="Название товара")
    category = models.ForeignKey(Category, verbose_name="Категория", on_delete=models.SET_NULL, null=True)
    vendor_code = models.CharField(max_length=20, verbose_name="Артикул", default=default_vendor_code)
    image = models.ImageField(upload_to="catalog/", verbose_name="Изображение", default="catalog/default.png")
    width = models.PositiveIntegerField(verbose_name="Ширина", help_text="Указывать в мм")
    height = models.PositiveIntegerField(verbose_name="Высота", help_text="Указывать в мм")
    length = models.PositiveIntegerField(verbose_name="Длина", help_text="Указывать в мм")

    class Meta:
        ordering = ["title"]
        verbose_name = "Товар"
        verbose_name_plural = "Товары"

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('product-detail', args=[str(self.id)])

how to download the nomenclature from 1s from xlsx?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-02-14
@BashAnka

  1. Install the xlrd module ;
  2. Parse the excel file with it;
  3. Create records in the database according to the data received from the file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question