Skip to main content

Django Mixin allows easy import of files into Django models.

Project description

django-import-file

Not ready for use yet!

Developed by Jakub Jadczak, 2024

Examples of How To Use with Django Class Based View

Simple Usage

Read csv

from django_import_file import FileImportMixin



class ImportFile(FileImportMixin, FormView):

    model = RowsData

    form_class = ImportFileForm

    template_name = "main/home.html"

    file_extension = "csv"

    file_encoding = "utf-8"

    delimiter = ";"

    required_columns = ["name", "age", "email", "phone", "address"]

    messages_success = "Import successful with no errors."

    success_url = reverse_lazy("main:home")

Read xlsx

class ImportFile(FileImportMixin, FormView):

    model = RowsData

    template_name = "main/home.html"

    file_extension = "xlsx"

    required_columns = ["name", "age", "email", "phone", "address"]

    sheet_name = "Sheet1"

    messages_success = "Import successful with no errors."

    form_class = ImportFileForm

    success_url = reverse_lazy("main:home")

With additional calculation method

class ImportFile(FileImportMixin, FormView):

    model = RowsData

    form_class = ImportFileForm

    template_name = "main/home.html"

    file_extension = "csv"

    file_encoding = "utf-8"

    delimiter = ";"

    required_columns = ["name", "age", "email", "phone", "address"]

    messages_success = "Import successful with no errors."

    success_url = reverse_lazy("main:home")



    def calculate_district(self, row):

        # name after _ must be equal to field in model, you want to calculate

        if ...:

            return "..."

        else:

            return "..."



    def get_file_content(self, file_path: str):

        df =  super().get_file_content(file_path)

        df["age"] = df["age"] + 10

        return df

Options:

#import

from django_import_file import FileImportMixin



#Declarations, examples

file_extension = "csv"

use_verbose = True # map column using verbose_name (columns name in the file must be equal to verbose_names)

map_column = {"name": "Full Name"} # map your own columns name, {model_name: file_name}

sheet_name = "Sheet1" # used in xlsx

required_columns = ["age", "name"] 

exclude_columns = ["address"]

delimiter = ";"

file_encoding = "utf-16-le"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_import_file-0.0.6.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

django_import_file-0.0.6-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file django_import_file-0.0.6.tar.gz.

File metadata

  • Download URL: django_import_file-0.0.6.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for django_import_file-0.0.6.tar.gz
Algorithm Hash digest
SHA256 27484451ca7d85bde821f198bf9e705487cee560be402eed8f0c918c0fe0c42a
MD5 830121d6cc52b8a393297889c6967a8d
BLAKE2b-256 22652675baa7edd8bdac563f843b36622e6d11e6a3ea0cb5fcfb58251899edca

See more details on using hashes here.

File details

Details for the file django_import_file-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_import_file-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 13b6b076b154b2afccda318ca415e190e80fe7cb506b2ff00d1a303a250d08ba
MD5 01b841a9e2691afd936ea29bd4faa363
BLAKE2b-256 af3fecce6d8ce599e78bc2f3a1a7f0e07aa4f847576ce3807a7d721170f8bad2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page