Limit foreign key choices in select in an inline form in admin

Used request instance as temporary container for obj. Overrided Inline method formfield_for_foreignkey to modify queryset. This works at least on django 1.2.3. class RoomInline(admin.TabularInline): model = Room def formfield_for_foreignkey(self, db_field, request=None, **kwargs): field = super(RoomInline, self).formfield_for_foreignkey(db_field, request, **kwargs) if db_field.name == ‘inside_room’: if request._obj_ is not None: field.queryset = field.queryset.filter(building__exact = request._obj_) else: field.queryset = … Read more

Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields

I stumbled upon this and although I didn’t care about my data much, I still didn’t want to delete the whole DB. So I opened the migration file and changed the AlterField() command to a RemoveField() and an AddField() command that worked well. I lost my data on the specific field, but nothing else. I.e. … Read more

Foreign key constraints in Android using SQLite? on Delete cascade

Foreign key constraints with on delete cascade are supported, but you need to enable them. I just added the following to my SQLOpenHelper, which seems to do the trick. @Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); if (!db.isReadOnly()) { // Enable foreign key constraints db.execSQL(“PRAGMA foreign_keys=ON;”); } } I declared my referencing column as follows. … Read more

Basics of Foreign Keys in MySQL?

FOREIGN KEYS just ensure your data are consistent. They do not improve queries in sense of efficiency, they just make some wrong queries fail. If you have a relationship like this: CREATE TABLE department (id INT NOT NULL) CREATE TABLE employee (id INT NOT NULL, dept_id INT NOT NULL, FOREIGN KEY (dept_id) REFERENCES department(id)) , … Read more

Foreign key referring to primary keys across multiple tables?

Assuming that I have understood your scenario correctly, this is what I would call the right way to do this: Start from a higher-level description of your database! You have employees, and employees can be “ce” employees and “sn” employees (whatever those are). In object-oriented terms, there is a class “employee”, with two sub-classes called … Read more

Setting default value for Foreign Key attribute

I would modify @vault’s answer above slightly (this may be a new feature). It is definitely desirable to refer to the field by a natural name. However instead of overriding the Manager I would simply use the to_field param of ForeignKey: class Country(models.Model): sigla = models.CharField(max_length=5, unique=True) def __unicode__(self): return u’%s’ % self.sigla class City(models.Model): … Read more

Adding a column as a foreign key gives ERROR column referenced in foreign key constraint does not exist

To add a constraint to a column It needs to exists first into the table there is no command in Postgresql that you can use that will add the column and add the constraint at the same time. It must be two separate commands. You can do it using following commands: First do as: ALTER … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)