Migrations on Through Model Django Many to Manyo Field - django

I have a model used in a ManyToManyField that I want to change.
User
Teacher
- ManytoManyField through TeacherRelation
TeacherRelation
NewModel
I want to change the foreign key of the TeacherRelation so it uses NewModel instead of Teacher . I already have data That I dont want to loose on the db, so I would like to know what's the best way to make this kind of migrations. Im concerned about issues like:
Will I have to delete the previous entries on the db that used the other foreign key?
Is there a way to find all the previous entries and replace them with the correct new primary key? I think I'll have to write a script for that... But where and when should I place it and run it?
Thanks for the help.

you can manually duplicate the table. If you are using postgres, you can use PGadmin III or the console. for sqlite you ca send basic SQL commands to duplicate Teacher.
After Duplicating teacher, just change the original teacher table name to "NewModel" and build it as you wish :)
Easiest option IMO.

Related

Why there isn't a "ListField" in Django?

I have a model that has a Charfield (let's name it advantages) with a choices attribute. After a while, I've decided that this field should be "upgraded" to some kind of ListField, since more than one choice can be selected.
From what I have searched, I have two options:
1 - Create a new model, and use a ManyToManyField in the first model referencing this new model. This way, the "multiple select" default field used in admin will be rendered. Life is good.
2- Create a custom field that saves my field as a string with some kind of separator.
These two approaches are summarized in What is the most efficent way to store a list in the Django models? and the 2nd approach in more examples: How to create list field in django, http://cramer.io/2008/08/08/custom-fields-in-django/, https://djangosnippets.org/snippets/1200/, https://djangosnippets.org/snippets/1491/
Fact is: I don't want to create another model just to have the ManyToManyField. This is a controlled list of choices that I have (and don't want people adding new items) and think that creating a table for this is overkill (although I can create a fixture to this table and not register the model in admin.py, so people wouldn't be adding new items. But I don't know how would migrations work when changing these values in fixtures, when in the past I would just chance the choices tuple in my model definition).
...and creating a new custom field, I don't know. This seems like problems in the long run since I don't know the implications, problems when upgrading Django, etc.
Why there isn't a built in ListField? Which problems do you see in the long run for the two approaches I'm thinking of? I'm planning to do the first but I'm a little lost about migrations.
django.contrib.postgres has an ArrayField.
You seem to not be willing to create a new table with only the List inside (because it would be "overkill"), but what you are suggesting is to copy/paste the same exact values in all the entries of your table, which isn't a good model solution in my opinion.
I'd advise to go for the ManyToMany (or any other implementation doing the trick with another table).

Django -- add model to database without losing data

I have a simple Django website (just a form, really) which asks a few questions and saves the data in a SQL database using Model.save(). Pretty simple. I want to add a model to do page counting, though -- it'll just be a single object with a field that gets incremented each time the page's view function is called.
Now, I know little to nothing about SQL. I imagine this is not terribly difficult to do, but I would like to avoid losing or breaking all my data because of a slight misunderstanding of how the database works. So how can I go about doing this? I've heard of some third-party apps that will implement such functionality, but I'd like to do it myself just for learning purposes.
I don't understand why your existing data would be affected at all. You're talking about adding a completely new table to the database, which is supported within Django by simply running manage.py syncdb. The case where that doesn't work is when you're modifying existing tables, but you're not doing that here.
I must say though that learning and using South would be of benefit in any case. It's good practice to have a tool that can maintain your model tables.
(Plus, of course, you would never lose any data, because your database is backed up, right? Right?)
Since you're adding new model, you can just run syncdb and it will create new table for your model. If you were to change existing model, then you'd need to manually update database schema using "ALTER TABLE" statements or use South instead.

How do I make changes to a model in Django?

How does Django handle changes to my Model? Or, what help does it offer me to do this?
I am thinking of a situation where I have already have published data to my DB which I don't want to lose, but I need to make changes to my data model - for example, adding extra fields to a particular class, changing the types of fields, etc. My understanding is that syncdb won't ever alter tables that already exist in the DB.
For example, let's say I have the following model:
class Person(models.Model):
name = models.CharField(max_length=200)
phone_number=models.CharField(max_length=200)
hair_colour=CharField(max_length=50)
Things I might want to do to Person off the top of my head:
I wish to add an 'age' field.
I realise I want to use IntegerField instead of CharField for phone_number (whether this is a good idea or not, is out of scope...) - assuming it's possible.
I realise that I no longer wish to define hair_colour 'inline' within Person, because several people share the same hair colour - I wish instead to change this to be a foreign key to some other model.
Whilst I can imagine some of these are tough/impossible for the framework to 'guess' exactly what needs to be done to my data if all I do is update the models.py, I can imagine that there might still be some tooling to help enable it - does it exist?
In particular I imagine there must be some good patterns for option 1.
I'm very new to Django and have no experience with any other ORM-type stuff, which I think this is - I've always been a bit suspicious of ORMs, mainly for the reasons above :)
Django itself will not attempt to modify an already created database table. What you are trying to do is typically called "Migration" and there are a couple of different Database Migration tools available for Django.
South
Schema Migrations
Data Migrations
Backwards Migrations
Nash Vegas
Schema Migrations
Data Migrations
Django Evolution
Schema Migrations
Data Migrations (Unknown)
Backwards Migrations (Unknown)
Of the three South is probably the most widely used but they each have different ways of dealing with migrations. You can see more details on the comparison on Django Packages.
Much of what you're asking about can be done with the django project South. You add it as an INSTALLED_APP. Create a baseline, then as your model changes it creates SQL statements to convert your tables and the rows with-in the tables to the new model format.

Django syncdb ignore a specific model

Is there a way to make syncdb ignore a specific table?
I decided to declare a M2M twice because from one model I wanted it to appear as a simple M2M that works with filter_horizontal. In another I added a through field and show it in an inline. I used db_table to make the simple one use the same through table. This all works well usually, BUT in syncdb I always get an error the first run because it thinks it must create the table twice, but it already exists the second time. Not a problem until I get to testing which creates the test database and fails. Maybe I'm just an idiot for using this hack. Is there anyway to make Django ignore this error or specify not to create the m2m through table the second time?
I ended up using the Model Meta option managed = False to make syncdb ignore the model. Then used initial sql data to create the sql for the table by hand. If I could tell django that only the manytomany field was not to be managed I wouldn't need to write custom SQL, but this can't be done.

Making a django Fixture that gets 100 items from each Table, or follows foreign keys

I need to create some fixtures for django test. Does anyone know of a shortcut that lets me get x number of rows from every table in database and serialize it?
Background:
I have Multiple tables with 10's of millions of entries. I have tried to use the ./manage.py dumpdata, but in addition to taking too long there is no way fixture should be that large.
Each table has multiple foreign keys.
The Problem:
The code I am trying to test frequently calls select_related() Meaning I need all the foreign key relationships filled in.
Does anyone know of any tools that can help me follow foreign relationships for serializing DB data??? Any suggestions would be greatly appreciated.
Thank you for your time.
I have used the django-autofixture pluggable apps in a couple projects. You could give that a shot. Instead of using data from your database for tests, create a development database filled with autofixtures.
This link has a few other examples of similar pluggable apps.
http://djangopackages.com/grids/g/fixtures/
Another option is the tool Dynamic Dynamic Fixture, that follow Foreign Keys and Many to Many fields. Also, you can use the option "number_of_laps" that may help you.