logo
down
shadow

Django - Efficiently bulk create inherited models


Django - Efficiently bulk create inherited models

By : user2954764
Date : November 22 2020, 10:31 AM
Hope this helps The workaround I settled on was wrapping all of my collected create()s in a with transaction.atomic():. This greatly reduced running time by not opening any database connections or executing any queries until all of the Python had returned.
A downside could be that if any errors at all are encountered all changes are rolled back and the database is untouched. This could be remedied by chunking the create()s into batches and opening and closing a transaction around each one. (In my case this was not the desired behavior because I wanted all of the data or none of it.)
code :


Share : facebook icon twitter icon
In Django, how could one use Django's update_object generic view to edit forms of inherited models?

In Django, how could one use Django's update_object generic view to edit forms of inherited models?


By : pramit
Date : March 29 2020, 07:55 AM
this will help Alright, here's what I've done, and it seems to work and be a sensible design (though I stand to be corrected!).
In a core library (e.g. mysite.core.views.create_update), I've written a decorator:
code :
from django.contrib.contenttypes.models import ContentType
from django.views.generic import create_update

def update_object_as_child(parent_model_class):
   """
   Given a base models.Model class, decorate a function to return  
   create_update.update_object, on the child class.

   e.g.
   @update_object(Animal)
   def update_object(request, object_id):
      pass

  kwargs should have an object_id defined.
  """

  def decorator(function):
      def wrapper(request, **kwargs):
          # may raise KeyError
          id = kwargs['object_id']

          parent_obj = parent_model_class.objects.get( pk=id )

          # following http://www.djangosnippets.org/snippets/1031/
          child_class = parent_obj.content_type.model_class()

          kwargs['model'] = child_class

          # rely on the generic code for testing/validation/404
          return create_update.update_object(request, **kwargs)
      return wrapper

  return decorator
from mysite.core.views.create_update import update_object_as_child

@update_object_as_child(Animal)
def edit_animal(request, object_id):
  pass
urlpatterns += patterns('animals.views',
  url(r'^edit/(?P<object_id>\d+)$', 'edit_animal', name="edit_animal"),
)
Django forms: most DRY way to organize create / update forms for inherited models

Django forms: most DRY way to organize create / update forms for inherited models


By : living free
Date : March 29 2020, 07:55 AM
around this issue You're right that the class-based view documentation is rather primitive. No doubt it will improve, but for the moment you need to be prepared to experiment and to read the source.
You are trying to do two things:
code :
from django.views import generic

class PostCreateView(generic.CreateView):
    form_class = PostForm
    model = Post

class PostUpdateView(generic.UpdateView):
    form_class = PostForm
    model = Post

class BlogPostCreateView(generic.CreateView):
    form_class = BlogPostForm
    model = BlogPost

class BlogPostUpdateView(generic.UpdateView):
    form_class = BlogPostForm
    model = BlogPost
from django.views import generic

class PostView(generic.FormView):
    form_class = PostForm
    model = Post

class PostCreateView(PostView, generic.CreateView): pass
class PostUpdateView(PostView, generic.UpdateView): pass

class BlogPostView(PostView):
    form_class = BlogPostForm
    model = BlogPost

class BlogPostCreateView(BlogPostView, generic.CreateView): pass
class BlogPostUpdateView(BlogPostView, generic.UpdateView): pass
Django AutoSlugField across inherited models

Django AutoSlugField across inherited models


By : jane doe
Date : March 29 2020, 07:55 AM
should help you out OK so after digging through the source code it turns out the docs have updated since I lasted looked.
So, if you add objects = models.Manager() to your BaseModel and pass that to the AutoSlugField. This will check the slug against BaseModel rather than the sub class.
code :
class BaseModel(models.Model):
    objects = models.Manager()
    name = models.CharField(max_length=255)
    slug = AutoSlugField(populate_from='name', unique=True, db_index=True, managers=objects)
    # ... other fields
Django: How to save inherited models?

Django: How to save inherited models?


By : Tommy Karlsson
Date : March 29 2020, 07:55 AM
this will help Because you haven't declared your Conversation model to be abstract. You're using multi-table inheritance. Have a look at the docs.
If you want all the data stored in your child then you should do something like -
code :
class ConversationBase(models.Model):
    contact             = models.ForeignKey(Contact)
    conversation_datetime = models.DateTimeField()    
    notes               = models.TextField(_(u'Notes'),        blank=True)  

    class Meta:
        absract = True

class Conversation(ConversationBase):
    pass

class ConversationHistory(ConversationBase):
    log_date_time = CreationDateTimeField()
Django bulk create for models with multiple required fields

Django bulk create for models with multiple required fields


By : Egor Babosiuk
Date : March 29 2020, 07:55 AM
I hope this helps . I have a Person model, a Demographic model, and a PersonDemographic model which contains detailed methods using data from both models, and saves some very expensive calculations as a field for specific person-demographic instances. How do I use bulk create to facilitate entries into my PersonDemographic model? , You can do it like this:
code :
demographic = Demographic.objects.get(id=7)
PersonDemographic.objects.bulk_create(
    [PersonDemographic(person=person, demographic=demographic) for person in Person.objects.some_query_set()]
)
Related Posts Related Posts :
  • How to Select record with the maximum value in a GROUP BY?
  • MySQL Innodb Full text Proximity Search Gives Horrible Performance
  • MySQL best approach for db normalising, relationships and foreign keys
  • How to update one table based on the count value of another table while matching ids?
  • MAMP Pro 5.1.1-terminates MySQL when quitting
  • SQL - Get date of most votes cast
  • "Fuzzier" search in mysql
  • Tracing mysqld Using SystemTap
  • SELECT + INSERT + Query Cache = MySQL lock up
  • SQL vs. NoSQL for medium complexity search systems
  • SQL Error when creating table
  • Is it possible to write a query to compare rows to other rows in same table?
  • Display all duplicate records based multiple columns
  • summing time column in mysql
  • InnoDB: Cannot open table
  • Why is IN() considered O(logN) operation?
  • Can a deadlock occur on commit?
  • Is there a fast way to do a '%phrase%' search in any DB?
  • Is using an IN over a huge data set a good idea?
  • How can I know if a table is already arranged in ascending order in MySQL?
  • MySQL - Designing database for a simple game
  • Why am I getting "Host '192.168.1.220' is not allowed to connect to this MySQL server"?
  • what type of password hash? possible mysql hash?
  • MySQL - Using a date range vs functions
  • Using IS NULL and COALESCE in OrderBy Doctrine Querybuilder
  • How to merge MySQL columns into one column if there are empty values in the table?
  • Cardinality Confusion
  • Renaming column multiple ids in SELECT
  • Group all from and to messages per user
  • How to handled the null values in WHERE IN condition in mysql
  • Is it okay to update primary key field to shift a record to the top?
  • Stop query through pdo
  • How to check whether particular value in field in MySQL exists
  • Execute a statement for every table in a database
  • insertion 74G data into mysql table cost more then 2 days, how to improve insert performance
  • Where can I see the differents "Hibernate Console Configurations" in Eclipse
  • MySql | relational database vs non relational database in terms of Performance
  • Exporting mysql database using mysqldump including procedures
  • Trigger MySQL not working
  • How do I update values for individual rows quickly?
  • MySQL join query with multiple where condition
  • MySQL stored procedure is not working
  • MySQL - Order By exact match at the end
  • Select max date or null date from same table MySQL
  • Best solution workaround for needing a mysql event on insert?
  • MySQL select several columns of several tables at the same time without using JOIN
  • Is it more efficient to have a table with one of its columns mostly empty or make a new table and refer to it?
  • mysql data truncated with ñ character
  • MySQL Distinct Active Users for the last month query
  • Get all rows that match another attribute in the same table
  • MySQL statement to get min value with max ID
  • DATEDIFF Current/Date for Last Record
  • What is the logic or procedure for combining multiple DB query's results into one elegant table?
  • Modify TIMESTAMP MySQL for use as Primary Key
  • MySQL Dual table Join Dual table
  • Ruby on Rails - database where sorted by id
  • MySQL 5.6 : Access denied for user 'root' @ 'localhost' when password contains backslash
  • sql query case dateadd
  • Click on one button multiple times and mySql
  • Select daily amount of specific values from same column
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org