Can Django automatically create a related one-to-one model?

Take a look at the AutoOneToOneField in django-annoying. From the docs: from annoying.fields import AutoOneToOneField class MyProfile(models.Model): user = AutoOneToOneField(User, primary_key=True) home_page = models.URLField(max_length=255) icq = models.CharField(max_length=255) (django-annoying is a great little library that includes gems like the render_to decorator and the get_object_or_None and get_config functions)

How to set a default attribute value for a Laravel / Eloquent model?

An update to this… @j-bruni submitted a proposal and Laravel 4.0.x is now supporting using the following: protected $attributes = array( ‘subject’ => ‘A Post’ ); Which will automatically set your attribute subject to A Post when you construct. You do not need to use the custom constructor he has mentioned in his answer. However, … Read more

How to automatically create an initializer for a Swift class?

Update As of Xcode 11.4 You can refactor (right-click mouse menu) to generate the memberwise initializer for class and struct. Note that struct automatic initializers are internal. You may want to generate memberwise initializer when defining a module to make it public. Right-click > Refactor > ‘Generate Memberwise Initializer’ For older Xcode There are handy … Read more

Rails has_many with dynamic conditions

Rails 4+ way (Thanks to Thomas who answered this below): has_many :faixas_aliquotas, -> (object) { where(“regra_fiscal = ?”, object.regra_fiscal) }, :class_name => ‘Fiscal::FaixaAliquota’ Rails 3.1+ way: has_many :faixas_aliquotas, :class_name => ‘Fiscal::FaixaAliquota’, :conditions => proc { “regra_fiscal = #{self.regra_fiscal}” } Rails 3 and below: has_many :faixas_aliquotas, :class_name => ‘Fiscal::FaixaAliquota’, :conditions => [‘regra_fiscal = #{self.regra_fiscal}’] No. This … Read more

Email model validation with DataAnnotations and DataType

DataType attribute is used for formatting purposes, not for validation. I suggest you use ASP.NET MVC 3 Futures for email validation. Sample code: [Required] [DataType(DataType.EmailAddress)] [EmailAddress] public string Email { get; set; } If you happen to be using .NET Framework 4.5, there’s now a built in EmailAddressAttribute that lives in System.ComponentModel.DataAnnotations.EmailAddressAttribute.

How I can put composite keys in models in Laravel 5?

I wrote this simple PHP trait to adapt Eloquent to handle composite keys: <?php namespace App\Model\Traits; // *** Adjust this to match your model namespace! *** use Illuminate\Database\Eloquent\Builder; trait HasCompositePrimaryKey { /** * Get the value indicating whether the IDs are incrementing. * * @return bool */ public function getIncrementing() { return false; } /** … Read more