Non-overlapping constraint on Django postgres IntegerRangeField - django

I found that Django has support for Postgres' range fields and also found that in Postgres, we can have non-overlapping constraint (exclusion constraint) as described here at section 8.17.10.
Can anyone tell me how can I apply this constraint in Django in Field iteself not in migration file.
I am using django 1.8 and postgres 9.4
Your help will be highly appreciated.

I am not sure you can create a constraint without migration file ( going to the database by consequence ) but as an option, you can use Django fields validations that will raise exceptions in case you validations don't pass
https://docs.djangoproject.com/en/2.0/ref/validators/

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.

Do i need models.py even for ready made mysql databases?

I spin up a django project. Afterwards, i didn't write models.py but instead I created a database from MySQL command line(independent from django) and created three tables with required columns. Finally i connected my django app with that database successfully. I applied migrations. But now i am confused do i need to write models.py with every field name as in column?
I remember implementing a basic project in which i did write models.py and created database using "python manage.py shell" and then put values using
"from polls.models import Choice, Question"? How do i put data now initially and then using python on some action from UI?
Do i need models.py even for ready made mysql databases?
You do not need to construct models. Some (small) webservers are even completely stateless, and thus do not use a database. But a large part of how Django can help you is based on models.
You can write your own queries, forms, etc. But often by using a ModelForm, Django can for example remove a large amount of boilerplate code. It will make it furthermore less likely that there are mistakes in your code. So although not strictly necessary, the models are usually a keystone in how Django can help you.
You can use the inspectdb [Django-doc] command to inspect the database, and let Django "sketch" the models for you. Usually you will have still some work. Since Django can, for example, not derive that a field is an EmailField, since both a CharField and EmailField look exactly the same at the database side.
You do not need to use inspectdb however. You can construct your own models. If you create your own models, but these exist already at the database side, you might want to set managed = False [Django-doc] in the Meta of your model, to prevent Django from constructing migrations.

How to make Postgres to manage the table’s lifecycle in a django project

I'm trying to create a project using django with a populated database, my problem is when I try to create a new object I get this error duplicate key value violates unique constraint, because the database is already populated, what can I do to change it to make Postgres manage it or django to get the right sequence?
thanks in advance
It's not entirely clear what you're asking. Are you getting the errors because Django tries to create tables that already exist in your database? If so, you can add managed = False to your model's Meta class and Django will not touch the database for this model. It will then be your own responsibility to keep your tables up to date with your Django models.
See the documentation here: https://docs.djangoproject.com/en/2.0/ref/models/options/#managed .

How to fix django admin foreign key display that used raw_id_fields

I have recently migrated a website from Django 1.6 to Django 1.8 and I can't figure out the simple way to fix the behaviour of django admin when it displays foreignkey fields that use the "raw_id_fields" in the adminClass.
The use case is exactly the one from django book : http://www.djangobook.com/en/2.0/chapter06.html (Figure 6-14)
class BookAdmin(admin.ModelAdmin):
...
raw_id_fields = ('publisher',)
Administration display used to have a spyglass at the end allowing to pick a new foreignkey id, but now none is displayed.
Documentation hint's at some new mechanism but it's unclear how to apply it here:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
Thanks
As often, the problem was elsewhere, the statics from the admin had not been updated and i was using an older version of admin static files.
a simple python manage.py collectatic command fixed this issue.
For future reference, any issue with admin should start by making sure statics are up to date.

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

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 (