Django - DEV django bulk update/add m2m field ... how to exactly? - django

I need to bulk add m2m fields and looks like django dev version supports bulk.
But they only give example of how to add a single object? I cant find any examples of it being done in bulk.
Also is this method more effective then solution proposed here?
http://grokbase.com/t/gg/django-users/158c8q3ty3/bulk-add-m2m-relationship-for-multiple-instances (

Related

JsonField in Django for MySQL

I currently use django 3.2 and MySQL as database, I want use MySql Json Field. For this reason use django-mysql third party package in my project.
After create model got this Warning: (django_mysql.W004) django_mysql.models.JSONField is deprecated. HINT: Use django.db.models.JSONField or django-jsonfield-backport instead.
If i use django.db.models.JSONField as json field, django use specific MySQL's JsonField ? Dose any effect on preformance? Which one has best performance on database?
The documentation here, does not mention a difference in performance. So, probably there is none. It looks like the change is only for supporting all databases with the same unique model field. There is a similar issue in this link. So, you can check that, too. As I read, django-jsonfield-backport is for early versions of Django. So, choose according to your version.

How to add custom constraints to ForeignKey relationships in django

Say I wanted to add an extra attribute to the ForeignKey relationship in django to allow further customization.
Say for instance I want to add an attribute which when enabled will not allow repetitions ( I realize this particular use case already has a fix but it's just an example).
How would I approach such a thing?
It appears that the next version of django will contain the ability to define them on the model.
In the meantime, I think you'll need to create an empty migration and have it create the constraint manually via migrations.RunSQL

Add uuid field in django Existing User model?

I am using Django Rest framework for my application. I almost implemented registration and login process using django User Model without customizing it now i want to add a uuid field in it. Is there any way we can just add uuid field and other process remains same?
Adding UUIDField to existing models is not so easy but you can read an How-To in the official Django documentation in the specific section (I updated it few months ago) :
Migrations that add unique fields.

DjangoRestFramework HTML views timeout

I have created an application that uses django-rest-framework. The problem is that in production with lots of data, the rendering of HTML pages will timeout. This is caused, I believe, by the select fields that represent ForeignKey of the model that take too long to render when all the production data is available. What is the most approriate way to prevent this?
As far as I understand, the problem is with the selectbox loaded with tons of items. The solution that is being used in django admins is to use "raw_id_fields" for the choicefields (or foreign keys) that have lots of items.
Unfortunately, DRF doesn't support Raw ID fields for now. However, you can implement a similar approach by using autocomplete fields. Right now there isn't built-in support, but you can use some external packages as described in DRF's official documentation: http://www.django-rest-framework.org/topics/browsable-api/#autocomplete
You should use select_related()/prefetch_related queryset methods to fetch the associated objects, which fill your selects. Post your models, serializer and a queryset so we can make a real example.

Integrating Django tutorial example Polls app and django-registration

I'm learning Django on Ubuntu 13.04, Python 2.7, Django 1.5, Postgres 9.2,
Bootstrap 3.0. I'd like to achieve a combination of the tutorial example Polls app with django-authentication.
As my first effort I got the Polls app working from the Django 1.5 tutorial. I then installed django-registration 1.0 and these templates to make it work. I chose that package for authentication as opposed to django-allauth as a result of my question on authentication framework.
Now I want to integrate Polls and django-registration to record a set of results per user. After the poll results have been collected the admininstrator uses Django Admin interface to run a script to analyse the results (e.g. compute some statistics) and send an email to a subset of all users.
I briefly looked at two existing projects that looked like could get me there out of the box.
Light Bird's Questionnaire App was too complicated using a custom library of modular class based views. I'd like to keep it as simple as possible, using as much of out-of-the-box Django 1.5 functionality as possible for ease of maintenance and initial design.
Pinax web framework on top of Django, although a great idea, seems to be stuck in dark ages of 2011 with latest code supporting only Django 1.4 and Bootstrap 2.x. Starter projects don't look that useful and documentation isn't flash either.
Based on the above it looks like I'll have to do the integration of Polls and registration manually. At first pass I was thinking roughly the following:
The poll & choice could be simplified down to just a numeric answer to a question.
At database level we would need a separate table.
The primary key would be the userid.
Each column would store one answer per.
I'm guessing this would need a class PollsResults in model.py that would include defining the primary key as User, which should exist via django-registration.
Exactly how to do that and what follows gets a bit hazy to me at the moment.
I'm sure the above is a simple exercise for a Django developer. Could anyone give me some starting hints or even better an existing project that does something similar?
It looks like you're slightly underestimating the power of using a framework such as django. For example, you don't really need to worry too much about tables in the database or what will be their primary keys, because django's Object Relational Mapper (ORM) takes care of a lot of that for you.
If you want to connect two models (database tables) in django you can use a foreignkey like this:
class ThingOne(models.Model):
name = models.CharField(max_length=50)
class ThingTwo(models.Model):
thing_one = models.ForeignKey('ThingOne')
The quotes around 'ThingOne' in my ForeignKey are actually unnecessary because the ThingOne model has already been defined, but I like to use quotes anyway because it means your ForeignKeys will also work for models defined below (in your code) the model linking to them.
You therefore just need to add a relationship between your Polls and User models. If one user might have many poll results you should probably use a ManyToManyField instead of a ForeignKey but the principle is the same. That should be enough to get you started.