Using Django Json field to reduce migrations issue - django

I am new to Django Json Field. I have been creating models and migrating them for now. Now I am introduced to Jsonfield. What I have heard is, it is the best way to mitigate the migrations issue because of jsonfield. It is because, if we had to add fields or remove fields from the model after populating the fields (if we have used other regular fields like chafield and emailfield) in production, there may arrise migration issue which we can avoid if we use jsonfield becaue we can just pass any json data with any number of fields in the jsonfield. So, is this the best way to avoid migration issue?? I am seeking expert advice here, because there is no one I can ask and that is what I have heard.
It seems like this.
class Example(models.Model):
data = models.JSONField(null=False, default=dict)
So, instead of creating two models named Contacts and Feedback for saving the data of contact form and feedback form, I can simply use this same example model and validate to accept any data of many such forms existing in the frontend.

If you want to use JSON just to avoid migrations, then that's not a good idea.
Basically, there are these two rules for using JSON:
If the data doesn't have a strict structure.
If you don't need to query (filter, search, order, etc.) the database using the given data.
Consider this example:
class User:
email = EmailField()
address = JSONField()
The email is in a separate field because we want to easily query the database to check for duplicate sign-ups.
The address is in a JSONField because we won't need to query the database using address data.
However, some applications may require to query using address, for example, to list all users from a particular city. In that case, using JSON will be a bad choice.

Related

How to dynamically swap default database on the model manager in django?

I am creating a project in django and django rest framework. Its an api for an angular app. The database setup consists of multiple databases. one is default database, all the django tables reside in this database; rest of the databases belong to a type of a user, each user is supposed to have a separate database. So, all the user related data goes to its separate database. To implement the selecting database dynamically, user object has an extra field to store the database to write to.
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
"""Custom User model."""
database= models.CharField(max_length=9)
Reason for doing this was performance improvement as each database is separate, ListView and DetailView would work faster than if the data was stored in the one database only.
I know I can choose a database to store by using the using method on the model manager. In the rest api things work fine and data is being stored in their separate databases, but I end up overriding methods that django has defined. Its adding development cost to the project. Foreign keys and ManytoMany keys needs to be resolved with the current database of the user, which is not happening as I have customized the database setup. Also, my code cant be as good as theirs :p , as they have written django over the course of many years.
I have overwritten many querysets already, but django still uses default database many times. If only I could use the request object in the model manager of django models to swap the default database on per request basis, things would be different i think.
My questions are -
Is there a way to access the request object in the model manager? I could do something to the effect of below code.
class CustomManager(models.Manager):
def get_queryset(self, request):
return super(CustomManager, self).using(request.user.database).get_queryset()
Model manager has _db property that could be used to select database. Would overriding it is advised? if yes, how and where in the code?
Is there a better way to implement the separate databases?
Thanks in advance.
Regards
Using a database router is recommended in Django docs, but the problem is it only accesses the model class.
Found a couple of questions related to the problem of switching databases dynamically. This post has a solution that would solve the problem of passing the request.user or any other parameter by using a threading.local instance.
Someone created a reusable plugin even for this - https://github.com/ambitioninc/django-dynamic-db-router
Hope that helps.

Do all tables need to be built as models?

I am new to Django and trying to figure out models. I have a question that I just cannot find the answer to. I am designing an equipment checkout app. I would like to have a data entry page (/Admin) where we can enter the equipment names and types...no problem (I think). However, I also need a Checkout table that will be updated as equipment is checked in and out and an Employee table that would hopefully pull from AD. My question is, do these two tables that will not be updated manually on the Admin page still need to be defined as models? Is this where the 'EDITABLE' field option comes in? Thanks!
Models in Django are the main interface to communicate with data layer. They map directly to database, provide access to it using ORM and also track changes via migrations. They can be integrated with pretty much all Django components: generic views, forms, serializers, etc.
Just because certain piece of data is not being updated manually, doesn't mean that you don't need to define models for it.
If you want to use database tables separately from models, you'd need to manually create DB connection and use raw SQL or create custom data model. Unless you're using a third party external service, I don't think there are many use cases where that would be required.
well i would do it like that
models.py
class Equipment (models.Model):
name = models.CharField(max_length=30)
types = models.CharField(max_length=30) #choice field here would be better
serial = models.CharField(max_length=30)
employee = models.ManyToManyField("Employee")
class = Employee (models.Model):
#here you add all employee data
this way you could create an instance from the admin for every equipment and employee and on every checkout you can update the equipment

Django Rest Framework Hyperlinked Fields Understanding

I can't seem to grasp the difference between HyperlinkedIdentity and HyperlinkedRelated Fields. I have a few questions that I can't seem to find the answers to online.
What is the actual difference? When would I want to use one vs. the other.
My next question is say I have 2 models, Project and Task.
A Task has a ForeignKey to Project. If I wanted the Project to hyperlink to the tasks within it, which Hyperlink field would I put in the ProjectSerializer? And what field would I put in the TaskSerializer to complement the ProjectSerializer assuming I wanted the tasks to be able to hyperlink back to the Project they are related to?
What is the difference between using the hyperlinked fields vs. just using regular nested serializers? When using hyperlinked fields, can I still filter by pk/id?
Last, What if a model had two hyperlinked relations in the serializer? From what I understand it creates a url field for each hyperlink, would it create two url fields in this case?
Thanks for any clarification you can offer, it will be a huge help towards cementing my understanding on the subject and allowing me to complete my API for my project.
What is the actual difference? When would I want to use one vs. the other.
HyperlinkedIdentityField is an hyperlink field for the current object itself while HyperlinkedRelatedField represent an hyperlink to other instances.
A Task has a ForeignKey to Project. If I wanted the Project to hyperlink to the tasks within it, which Hyperlink field would I put in the ProjectSerializer? And what field would I put in the TaskSerializer to complement the ProjectSerializer assuming I wanted the tasks to be able to hyperlink back to the Project they are related to?
HyperlinkedRelatedField is what you're looking for.
What is the difference between using the hyperlinked fields vs. just using regular nested serializers?
Hyperlinks can be browsed independently from the original resource. Handy if one of them belongs to another system. For example, you'll likely want to use hyperlink to tweets rather than let your server fetch them and them return them nested. Hyperlinks also allows the client to deal with its own caching rather than sending back all the data. Could be handy in case of fetching a list of items that nest the same user.
On the other hand, hyperlinks increase the network request count because it needs to fetch more data.
When using hyperlinked fields, can I still filter by pk/id?
Not sure what you mean here.
What if a model had two hyperlinked relations in the serializer? From what I understand it creates a url field for each hyperlink, would it create two url fields in this case?
Correct. hyperlinked relation are just a representation of a relation. It provides an hyperlink (an uri) to fetch the associated object.
This is useful because you won't need to know the pattern to fetch an object from the id: with a PrimaryKeyRelatedField you'll get the id but are missing the url to fetch the associated object.
This also allows the server to manage its own uri space without the need of updating the clients.
Hope this will help.

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.

How do I take user input and then sum/add to the database in django

I am trying to get the user input and add to the database.
Example:
A user is going to type an integer, then this is to be added to the current value in the database. It's like sending money to someone via online banking.
Assuming you want to use Django's ORM, you will need to define a model. In your model you will have fields that define the data items to store. After you've had Django's manage.py tool setup the database, you can create instances of your model saving them with the save method of your model instance.
This probably seems like a lot of hand waving, and it is. What you should do is work through the Django tutorial as it will answer your questions. The tutorial is at the Django documentation site.