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
Related
im here trying to start a new project, in django
and i came across this problem, while creating the models
My users will be different types of users, so i was thinking, should i use proxy models for it?
because here is how it´s going to be the realtionship between them.
i will have a client, and a personal trainer, and the admin of course, but the problem here is that the client is going to have a ForeignKey with personal trainer, my main question is, can i create in a proxy model an extra field where i establish the relationship with personal trainer proxy model?
If not, is there any other way that i can aproach this type of situation
Thanks in advance
No. As far as I know a Proxy Model only changes the behavior of a model. You can add new methods only. If you want to add new fields it must be a Concrete Model.
You should create a new model class which inherits from AbstractUser. Then you can either add a type field which can be "client" or "personal_trainer" or "whatever". Or you could use a OneToOne field to have a UserProfile associated w/ your user - a different profile for "clients" or "personal_trainers" or "whatevers".
I am trying to implement a model where only a subset of the fields should be presented in forms, based on a foreign key in the model.
The total number of fields is relatively low (~20), but may change frequently with different values of the foreign key.
I would like to use something like single table inheritance, but with a single model (no inheritance). I looked at existing packages eav, dynamic models... but they did not seem to fit my needs.
I part of the requirement is that I would like to query the models by sql without too many joins.
Here is a use case representing applications to grants based on a request for application (rfa). The rfa is entered in the admin by staff and the applications are made by end users.
application class
rfa (request for application): foreign key
field_1
...
field_20
rfa class:
app_fields (coma separated list of field names)
In the forms, only the fields in app_fields should be visible for a particular rfa.
I have the rfa part covered with django-separatedvaluesfield and I was wondering how to implement the application pages so that they work with generic views in the frontend and admin, and be DRY.
For the update views, I could have a method in the application model, but it would not work for create as the rfa is not defined for create:
def get_current_app_fields(self):
return self.rfp.app_fields
Any ideas on the best DRY strategy to implement frontend views and admin?
Thanks.
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.
In my project I'm trying to create central DB service with multiple database so here my question is can we create relationship between tables from two different databases?
Example:
MySQL DB1.table user
class User(models.Model):
name = models.CharField()
MySQLDb2.table post
class Post(models.Model):
title = model.CharField()
user= models.Forignkey(User)
Django doesn't support relationships across databases, and only officially supports a single schema within a database. While there may be ways to hack something like this up, depending on your flavor of database, it is not recommended.
I've done some work with hacks to have multiple schemata in Django, and it isn't pretty.
You may want to read this part of the documentation:
https://docs.djangoproject.com/en/2.0/topics/db/multi-db/#limitations-of-multiple-databases
Good luck!
I am in the process of converting a Webapp2 app to Django on Google App Engine. Everything is relatively straightforward, and the models have been converted from the webapp models to the django equivalents.
However, I feel this may be have been glossed over in the posts from the app engine team Refering This... Do I need to perform a data migration in order to re-use existing data, or can I simply use the existing NDB models somehow? (If so, what configurations are needed? I can't seem to figure this out).
there is no concept of a data migration in schemaless databases.
A migration that you think of is really creation or alteration of a database schema i.e the DB needs to have a schema only then would the idea of a migration make sense.
After looking into this a little further, I noticed that by default GAE would create db_tables with the default names as <app_label>_<model_name> (i.e. coreapp_GuestBook).
As a result, if you specify the model meta options in Django, matching the converted apps name with the original apps name, you will be able to access the same models with Django. Note that the field values may be inaccessible or possible corrupted if you did not do the conversion of webapp fields to the appropriate Django fields one for one.
See reference: https://cloud.google.com/appengine/articles/django-nonrel
For example, in my case, the Article app would be retrieved by specifying:
class Article(models.Model):
title = models.CharField(max_length=255)
class Meta:
db_table = 'Article'
verbose_name = 'Article'
verbose_name_plural = 'Articles'