Can we retrieve the field names from the models.py based on the column names in table from the database in a django project? - django-models

models.py: class Name(models.Model): name=models.TextField(db_column="Name_of_the_persion")
like this i defined the model, so that table 'Name' will be created on the database side, with column name as "Name_of_the_persion".
My requirement is like,I need to insert the new row in the table based on the django model field names instead of columns of tabel.
If any one knows, pls help me.
I tried inserting the data into table, by using psycopg2. But this was work in the case of pgadmin point of view only, that means its takes column names of database, instead of model fileds .

Related

Change Mapped table dynamically in Entity Framework

I am using Entity Framework. In my DB, I have a table (DOCMASTER) which is mapped in my model, and on occasion, has a backup from a different date created (e.g. DOCMASTER_10_01_2015). The mappings are identical, with the exception of the keys/constraints. These are not brought over with the backup.
I have an application with a dropdown that is filled with all of the tables in the DB that are of type "DOCMASTER". The user selects which table they would like to query from, and they search for a client in that particular version of the table.
What I would ideally like to do is remap my model to use the selected table instead of the mapped table, however when I do that using DbModelBuilder, it seems to want to remap all of the tables in that model, not just the one table. I receive the error "CodeFirstNamespace.CUSTOM1: : EntityType 'CUSTOM1' has no key defined. Define the key for this EntityType" wherein 'CUSTOM1' is my table name, but I receive this for all of my tables in the model.
I am debating just using a paramaterized query to query the selected table directly. Does anyone have any thoughts on this?

Adding antoher fields in TranslateBehavior in CakePHP

I use TranslateBehavior in my app.
Model translate fields like name, content and slug. The table has many records.
...and now I must add antoher field to this table and I have problem. When I added field name to actsAs in model, my records return empty results. Why?
How add another field to Translated model after fact?
At first, I suggest you try to clean the model cache (delete files from path/to/project/app/tmp/cache/models.
I ran into the same problem.
The solution was to execute a SQL query to create the new translated fields that should have been created before.
I had a title tarnslator field before. Now I want to add a company translated field.
My i18n table is translation.
My model is Speaker.
Here is the SQL query to execute:
INSERT INTO translation (locale, model, foreign_key, field)
SELECT locale,model, foreign_key, 'company' FROM translation
WHERE model="Speaker" AND field="title";

Trying to find name of TFS DB Table containing custom field data

I have a quick question, what is the name of the TFS 2010 database table that contains values for any custom fields.
I did a query against the TFS_Warehouse DB and the dbo.DimWorkItem table. However, I cannot find any of my custom work item fields under this table.
Can someone point me to the correct TFS 2010 table containing the custom field data? When I worked with Quality Center, the tables were pretty well defined so it was easy to do backend DB queries. TFS does not seem that intuitive.
Thanks
you have to add "reportable" to field definition.
Example - FIELD name="Scope" refname="xxx.Scope" type="String" reportable="dimension"
Wait few minutes and you'll see field in warehouse DB
look,
you need to go to your collection database, and to check a table called something like Fields.
there, you will find the new field properties and the type as well.
you can change the type to string and to be reportable.
go to the table of the WORKITEMLATEST, and check the field- you can see the name of the field like what was mentioned in the FIELDS table,.
open your work item normally, edit that field information, click save...
you can see your data updated in the WORKITEMLATEST table
BUT...
the problem is the STRING type is limited... I tried to add more text.. it keep telling me that number of character is over limit !

MVC 3 Entity Framework - new database column not populating corresponding property

I am creating a web app in MVC 3 using entity framework, and recent spec changes have required I add a new column to one of the database tables (a record creation date).
I have added the new column and populated it with values for my existing records, and updated the model in the entity framework. When I look at the object corresponding to the table with the new column, I can see a property for this new data: however the data for that property is not being retrieved.
Just to clarify:
I added column "CreatedDate" (datetime) to table "Orders" in my database
I populated this column with values for my existing records and set it to "not null"
I updated the model in entity framework
My model "Order" now has a non-nullable property "CreatedDate"
I populate a list of orders using LINQ ("from o in DB.Orders select o")
For any entry on my list, entry.CreatedDate returns "01/01/0001 00:00:00", not the value I have entered into the database.
What's going wrong?
EdmMetadata table?
Do you have an EdmMetadata table in the database? As far as I know, this table contains a key that is signed by the structure of the database. So if this value is not changed, the code could not be seeing the change. Good news is that this table is no longer needed:
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

Is using multiple tables an advisable solution to dealing with user defined fields?

I am looking at a problem which would involve users uploading lists of records with various field structures into an application. The 2nd part of this would be to also allow the users to specify fields to capture information.
This is a step beyond anything ive done up to this point where i would have designed a static RDMS structure myself. In some respects all records will be treated the same so there will be some common fields required for each. Almost all queries will be run on these common fields.
My first thought would be to dynamically generate a new table for each import and another for each data capture field spec.Then have a master table with a guid for every record in the application along with the common fields and then fields that specify the name of the table the data was imported to and name of table with the data capture fields.
Further information (metadata?) about the fields in the dynamically generated tables could be stored in xml or in a 'property' table.
This would mean as users log into the application i would be dynamically choosing which table of data to presented to the user, and there would be a large number of tables in the database if it was say not only multiuser but then multitennant.
My question is are there other methods to solving this kind of varaible field issue, im i going down an unadvised path here?
I believe that EAV would require me to have a table defining the fields for each import / data capture spec and then another table with the import - field - values data and that seems impracticle.
I hate storing XML in the database, but this is a perfect example of when it makes sense. Store the user imports in XML initially. As your data schema matures, you can later decide which tables to persist for your larger clients. When the users pick which fields they want to query, that's when you come back and build a solid schema.
What kind is each field? Could the type of field be different for each record?
I am working on a program now that does this sorta and the way we handle it is basically a record table which points to a recordfield table. the recordfield table contains all of the fields along with the field name of the actual field in the database(the column name). We then have a recorddata table which is where all the data goes for each record. We also store a record_id telling it which record it is holding.
This is how we do it where if each column for the record is the same type, then we don't need to add new columns to the table, and if it has more fields or fields of a different type, then we add fields as appropriate to the data table.
I think this is what you are talking about.. correct me if I'm wrong.
I think that one additional table for each type of user defined field for the table that the user can add the fields to is a good way to go.
Say you load your records into user_records(id), that table would have an id column which is a foreign key in the user defined fields tables.
user defined string fields would go in user_records_string(id, name), where id is a foreign key to user_records(id), and name is a string, or a foreign key to a list of user defined string fields.
Searching on them requires joining them in to the base table, probably with a sub-select to filter down to one field based on the user meta-data, so that the right field can be added to the query.
To simulate the user creating multiple tables, you can have a foreign key in the user_records table that points at a table list, and filter on that when querying for a single table.
This would allow your schema to be static while allowing the user to arbitrarily add fields and tables.

Resources