Check if a OneToOne relation exists in Django

So you have a least two ways of checking that. First is to create try/catch block to get attribute, second is to use hasattr. class A(models.Model): def get_B(self): try: return self.b except: return None class B(models.Model): ref_a = models.OneToOneField(related_name=”ref_b”, null=True) Please try to avoid bare except: clauses. It can hide some problems. The second way … Read more

Rails Models: how would you create a pre-defined set of attributes?

I assume that you are going to have more than a few of these multiple-choice attributes, and would like to keep things tidy. I would recommend the store it in the database approach only if you want to modify the choices at runtime, otherwise it would quickly become a performance hit; If a model has … Read more

Restrict django FloatField to 2 decimal places

If you are only concerned with how your FloatField appears in forms, you can use the template filter floatformat. From the Django Docs: If used with a numeric integer argument, floatformat rounds a number to that many decimal places. For example, if value = 34.23234, then in your template: {{ value|floatformat:2 }} # outputs 34.23

Rails Internationalization (I18n) in model validations: Possible or not?

The solution is to NOT include any custom message keys in the models, like… :message => I18n.t(‘activerecord.errors.models.my_model.attributes.whatever.please_select_whatever’) The model will then apply the default message keys, for example “:inclusion” in the case of “validates_inclusion_of” …and in config/locales/en.yml you need to have: en: activerecord: errors: models: my_model: attributes: whatever: inclusion: “Please select whatever.” # see default … Read more

rails model has_many of itself

This is a self-referential model, you can try something like this: class Event < ActiveRecord::Base belongs_to :parent, :class_name => “Event”, :foreign_key => “parent_event_id” has_many :child_events, :class_name => “Event”, :foreign_key => “child_event_id” end That way, you can call @event.parent to get an ActiveRecord Event object and @event.child_events to get an ActiveRecord collection of Event objects

How to implement Active Record inheritance in Ruby on Rails?

Rails supports Single Table Inheritance. From the AR docs: Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this: class Company < ActiveRecord::Base; end class Firm < Company; end class Client … Read more

How can I change a Django form field value before saving?

If you need to do something to the data before saving, just create a function like: def clean_nameofdata(self): data = self.cleaned_data[‘nameofdata’] # do some stuff return data All you need is to create a function with the name **clean_***nameofdata* where nameofdata is the name of the field, so if you want to modify password field, … Read more

Rails Model without a table

class Model include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :whatever validates :whatever, :presence => true def initialize(attributes = {}) attributes.each do |name, value| send(“#{name}=”, value) end end def persisted? false end end attr_accessor will create your attributes and you will create the object with initialize() and set attributes. The method persisted will tell there is … Read more

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