When you change the fields in the models in django - django

After you initially create the model and you want to change the fields in the models. If you syncdb ,it replies no fixtures found, meaning to say the models has not recognised the change. One way to solve this is to delete the database and recreate the database but you lose all the data. Is there any other better solution? Thanks...

Use south to help you for models changes. It will automatically detect any changes to models and generate appropriate script to update db schema.

Related

Django table model

I am relatively new to Django and would like to use it to build the back end of my app. I am using Django.DB models and Django rest-framework. I am trying to make a system where each company in my DB has a table that stores 3 records. Item, Price, and Description but I have no idea what type of field that would be or how one would do that. I was assuming that you would use a one-to-many field but there doesn't seem to something like that.
Thank You in advance.

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.

gender as a model from a SQL statement in django 1.8

I want to generate SQL code and take from that code, generating a Django model to avoid errors.
They will say that you first create the model and run the syncdb or migrate but my case is unlike the database is already created and I now want the model
Run this command to auto-generate models from an already existing database. But first make sure you've properly linked database to django app .
python manage.py inspectdb > models.py
Do check models.py file and make some changes if you something isn't rendered correctly.
For inspectdb approach, read this: https://docs.djangoproject.com/en/1.8/howto/legacy-databases/
Alternatively, you can write all the models by yourself and set managed = False. No database table creation of deletion will be executed by Django on this model. But it is somewhat complicated and puts some limits on model relationships.
For managed=False approach, read this: https://docs.djangoproject.com/en/1.8/ref/models/options/#managed

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.

how to sync django models with pre-existing database?

I am having a hard time trying to come up with a reasonable design for my project. I have an existing Postgres database that gets constantly updated from other Python scripts.
The web server built on Django framework will access the Postgres database to update User models only and display blog information for the logged in Users. The blog information is what is being updated overnight by other Python scripts.
Now my question, if I have to syncdb my blog model with existing Postgres database, would that cause any problem?
ex:
models.py
class Blog:
title=...
content=...
author=....
And say my Postgres db called mydb has many tables, one of which is blog table and contains columns for title, content and author.
How would make my model in sync with existing database?
Now lets say I included a new column in my db which is date of entry.
If I simply update my model to :
class Blog:
title=...
content=...
author=....
date of entry=...
will it work.
what are the potential problems here and any simpler solutions for these?
P.S: I have used South in the past. but the situation here is different. I am using db that is read-only from Django's point of view, and no data migration is necessary as of now.
If your database is read-only, you don't have to do syncdb. Use managed=False and the db_table meta option on your model to specify the table name it corresponds to, and likewise for the field column names.
If you haven't already, see the doc on legacy databases for more info.