Django’s ManyToMany Relationship with Additional Fields

Here is example of what you want to achieve: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships In case link ever breaks: from django.db import models class Person(models.Model): name = models.CharField(max_length=128) def __str__(self): # __unicode__ on Python 2 return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through=”Membership”) def __str__(self): # __unicode__ on Python 2 return self.name class Membership(models.Model): person = … Read more

Using ActiveRecord, is there a way to get the old values of a record during after_update

Ditto what everyone is saying about transactions. That said… ActiveRecord as of Rails 2.1 keeps track of the attribute values of an object. So if you have an attribute total, you will have a total_changed? method and a total_was method that returns the old value. There’s no need to add anything to your model to … Read more

DisplayNameFor() From List in Model

This actually works, even without items in the list: @Html.DisplayNameFor(model => model.Names[0].FullName) It works because MVC parses the expression instead of actually executing it. This lets it find that right property and attribute without needing there to be an element in the list. It’s worth noting that the parameter (model above) doesn’t even need to … Read more

Check if an object exists

I think the easiest from a logical and efficiency point of view is using the queryset’s exists() function, documented here: https://docs.djangoproject.com/en/stable/ref/models/querysets/#django.db.models.query.QuerySet.exists So in your example above I would simply write: if User.objects.filter(email = cleaned_info[‘username’]).exists(): # at least one object satisfying query exists else: # no object satisfying query exists

How to rename rails controller and model in a project

Here is what I would do: Create a migration to change the table name (database level). I assume your old table is called corps. The migration content will be: class RenameCorpsToStores < ActiveRecord::Migration def change rename_table :corps, :stores end end Change your model file name, your model class definition and the model associations: File rename: … Read more

How to update() a single model instance retrieved by get() on Django ORM?

With the advent of Django 1.7, there is now a new update_or_create QuerySet method, which should do exactly what you want. Just be careful of potential race conditions if uniqueness is not enforced at the database level. Example from the documentation: obj, created = Person.objects.update_or_create( first_name=”John”, last_name=”Lennon”, defaults={‘first_name’: ‘Bob’}, ) The update_or_create method tries to … Read more

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