Manipulating Data in Django’s Admin Panel on Save

From the Django Docs: ModelAdmin.save_model(self, request, obj, form, change) The save_model method is given the HttpRequest, a model instance, a ModelForm instance and a boolean value based on whether it is adding or changing the object. Here you can do any pre- or post-save operations. For example to attach request.user to the object prior to … Read more

How to use ‘User’ as foreign key in Django 1.5

Exactly in Django 1.5 the AUTH_USER_MODEL setting was introduced, allowing using a custom user model with auth system. If you’re writing an app that’s intended to work with projects on Django 1.5 through 1.10 and later, this is the proper way to reference user model (which can now be different from django.contrib.auth.models.User): class UserProfile(models.Model): user … Read more

How to persist an enum using NHibernate

Why are you guys over complicating this? It is really simple. The mapping looks like this: <property name=”OrganizationType”></property> The model property looks like this: public virtual OrganizationTypes OrganizationType { get; set; } The Enum looks like this: public enum OrganizationTypes { NonProfit = 1, ForProfit = 2 } NHibernate will automatically figure it all out. … Read more

How can one change the type of a Django model field from CharField to ForeignKey?

This is likely a case where you want to do a multi-stage migration. My recommendation for this would look something like the following. First off, let’s assume this is your initial model, inside an application called discography: from django.db import models class Album(models.Model): name = models.CharField(max_length=255) artist = models.CharField(max_length=255) Now, you realize that you want … Read more

MySQL terminology “constraints” vs “foreign keys” difference?

Yes, a foreign key is a type of constraint. MySQL has uneven support for constraints: PRIMARY KEY: yes as table constraint and column constraint. FOREIGN KEY: yes as table constraint, but only with InnoDB and BDB storage engines; otherwise parsed but ignored. CHECK: parsed but ignored in all storage engines. UNIQUE: yes as table constraint … Read more

Can you automatically create a mysqldump file that doesn’t enforce foreign key constraints?

The mysqldump command included with MySQL since version 4.1.1 by default produces a script that turns off the foreign key checks. The following line is included near the top of the dump file: /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; The /*!40014 … */ syntax is a conditional comment that will be executed on MySQL version 4.0.14 … Read more

How to change ForeignKey display text in the Django Admin?

If you want it to take effect only in admin, and not globally, then you could create a custom ModelChoiceField subclass, use that in a custom ModelForm and then set the relevant admin class to use your customized form. Using an example which has a ForeignKey to the Person model used by @Enrique: class Invoice(models.Model): … Read more

How can I INSERT data into two tables simultaneously in SQL Server?

Try this: insert into [table] ([data]) output inserted.id, inserted.data into table2 select [data] from [external_table] UPDATE: Re: Denis – this seems very close to what I want to do, but perhaps you could fix the following SQL statement for me? Basically the [data] in [table1] and the [data] in [table2] represent two different/distinct columns from … Read more

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