Django: get table name of a model in the model manager? - django

I have a model and have a model manager for this model. I'm writing some sql in the model manager, and I need the table name of the model in sql. I know the name of table is combined by metadata app_label and db_name, but is it possible that I can access them from manager class? I know I can create an model instance in the manager, but I would rather not do that..
Thanks very much!

Model manager has the field model:
Model.objects.model._meta.db_table

For a given instance, you can use instance._meta.db_table but that also works for the model class too, so if you can step up from the manager to its model, that'll work too

Let's say you have a model named Service
You can use
Service._meta.db_table # this will work too
This as well
Service.objects.model._meta.db_table # this will work too
This as well
Let's say you had an instance of the service Model like this
service = Service.objects.get(pk=1)
# get table name like this
table_name = service._meta.db_table # this will work too

Related

Dynamically change db_table django Meta

Sorry for my English
I have a table to manage list of database configurations. Table post in all db have same fields but different name. Eg. table post name is md_post, table post in other db is tdt_post.
So, when I access a db configuration I want to remote that database. I did it! But how can I change tb_name in Meta Django Model?
These Datatables configurations are non-permanent and may change when I add/update/delete record.
I tried it, but it only works one time.
Post._meta.db_table = `tdt_post`
Post._meta.original_attrs['db_table'] = `tdt_post`
When I change the db_table back to 'md_post', it doesn't work.
I have looked at the following posts, but it doesn't solve my problem:
Django model: change db_table dynamically,
Change table name for django model in runtime

How do you import a user model with strongloop arc database discovery

The loopback application already comes equipped with a user model and CRUDs; however, I'm trying to hook an old database up to my loopback application that already contains a user model so the model discovery replies with:
ValidationError: The `ModelDefinition` instance is not valid. Details: `name` is not unique (value: "User").
at /home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/dao.js:264:12
at ModelConstructor.<anonymous> (/home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/validations.js:483:13)
at ModelConstructor.next (/home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/hooks.js:75:12)
at done (/home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/validations.js:480:25)
at /home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/validations.js:554:7
at ModelConstructor.<anonymous> (/home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/validations.js:353:5)
at allCb (/home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/dao.js:1374:7)
at /home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:371:7
at /home/theory/.nvm/versions/v0.12.4/lib/node_modules/strongloop/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31
at process._tickDomainCallback (node.js:381:11)
Can I rename the incoming model to something else using model discovery? Is there a merge option for models?
This is because you're using a exiting model name (User) which belongs to loopback. Calling the model something like "UserAuth" would work.
Using slc arc if connected to the database when changing the model name you can migrate the old model to the new model.
To read more on why and how read their documentation here:
Using built-in models

Django routing to database based on model table

I have a Django (1.6) project with two databases. I have one app with one model, and multiple tables.
I want to use the database routers to set specific tables in the model to a specific database. All the documentation I have found seems to explain how to route a particular app to a particular database.
Looks like you could use a custom router and model attribute for this.
YMMV: Haven't tested this.
https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers
class MyModel(models.Model):
_DATABASE = "foo"
class CustomRouter(object):
def db_for_read(self, model, **hints):
database = getattr(model, "_DATABASE", None)
return database
# repeat for db_for_write, etc.

How to deal with many2many using the Django REST Framework

Using the Django REST Framework.
I have a many2many field in one of my models i.e.
attribute_answers = models.ManyToManyField(AttributeAnswers)
This creates its only table hence I don't have mapped in my models.
However, I want the API to allow a POST to this table, but it does not exist in models so in view and Serialize.py I cannot reference it. What is the recommendation for this?
This creates it's only table hence I don't have mapped in my models.
I assume you're talking about the reverse relationship, as attribute_answers will be accessible from the model instance that it's defined on, and will create an appropriate default serializer field.
For the reverse relationship, make sure you set the related_name on the model field, so that you can access the reverse relationship from the AttributeAnswers model.
See here: http://django-rest-framework.org/api-guide/relations.html#reverse-relations

How to insert data into 1-to-1 relationship tables in django

i'm new to django. I use django User and 2 extended tables (profile_table, more_table), each has a one-to-one relationship with User. So under models.py, i have 2 classes (one for profile_table, the other one for more_table). When I create a new user, i'll insert basic data into auth_user table and other information into profile_talbe and more_table. How and where to build this insert method? Should i use class manager? or overrite save function? or create a class method under profile_table class?
Thanks
If you create new instance from the profile model you should write static method in the profile model class.
Example:
class Profile(models.Model):
name = models.CharField(max_length=100)
#staticmethod
def create_profile(name=name):
profile = Profile(name=name)
profile.save()
return profile