1

My task is to upload a .csv file and dump the data in a postgresql database using django.

How can I create a table dynamically in postgresql using django to dump the .csv data into the database?

I have searched and found examples where we have to give attribute names while creating table but in my case I would not know the attribute names of the uploaded .csv beforehand.

How should I do the above?
I will be thankful if anyone can share some related links.

7
  • what's schemaless table?.. Commented Aug 22, 2017 at 9:22
  • I think it's ordinary CSV data with variable header line. This rules out all simple import option, because to import a CSV you have to create a table with column names and types. It's impossible to determine the correct types from a csv file without additional information. Commented Aug 22, 2017 at 9:30
  • @VaoTsun i mean is there any way of creating table in django dynamically without defining column names and types as user will be uploading different csvs with different attributes Commented Aug 22, 2017 at 9:37
  • @VaoTsun i mean is there any way of creating table in django dynamically without defining column names and types as user will be uploading different csvs with different attributes Commented Aug 22, 2017 at 9:37
  • Are you forced to use Postgresql? You can't create models dynamically in django, you'd have to do that yourself. Commented Aug 22, 2017 at 9:41

1 Answer 1

2

What you want is to create Dynamic Models. There is a very detailed wiki article about that:

  1. Django Dynamic Models which explains step by step how to achieve a dynamic model. A quick sample from that article:

    Implement a function able to build a model on demand:

    def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None):
        """
        Create specified model
        """
        class Meta:
            # Using type('Meta', ...) gives a dictproxy error during model creation
            pass
    
        if app_label:
            # app_label must be set using the Meta inner class
            setattr(Meta, 'app_label', app_label)
    
        # Update Meta with any options that were provided
        if options is not None:
            for key, value in options.iteritems():
                setattr(Meta, key, value)
    
        # Set up a dictionary to simulate declarations within a class
        attrs = {'__module__': module, 'Meta': Meta}
    
        # Add in any fields that were provided
        if fields:
            attrs.update(fields)
    
        # Create the class, which automatically triggers ModelBase processing
        model = type(name, (models.Model,), attrs)
    
        # Create an Admin class if admin options were provided
        if admin_opts is not None:
            class Admin(admin.ModelAdmin):
                pass
            for key, value in admin_opts:
                setattr(Admin, key, value)
            admin.site.register(model, Admin)
    
        return model
    

    Now you can parse your CSV file and decide about your fields and then create your model:

    import csv
    
    def csv_to_model(path_to_csv):
        with open(path_to_csv, "rb") as f:
            reader = csv.reader(f)
            col_names = next(reader)
    
        fields = {}
        for name in col_names:
            fields[name] =  models.CharField()
    
        return create_model(fields)
    

    Of course, you can make more complicated models. Read the wiki for a more thorough explanation.

  2. There exist this django package: django-dynamic-model which claims to add dynamic model creation on django (I cannot confirm that works):

    from dynamicmodel.models import DynamicModel, DynamicSchema, DynamicForm
    
     def csv_to_model(path_to_csv):
        with open(path_to_csv, "rb") as f:
            reader = csv.reader(f)
            col_names = next(reader)
    
        schema = DynamicSchema.get_for_model(MyModel)
        for name in col_names:
            schema.add_field(name=name, type='CharField')
    
        return schema
    
Sign up to request clarification or add additional context in comments.

Comments

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.