models

nested has_many :through in rails 3

廉价感情. 提交于 2019-12-06 05:11:56
问题 I know Rails doesn't support nested has_many :through relationships, though there's been talk and an open ticket about a patch since as early as Rails 2. I did come across a plugin that's pretty slick, but the master branches don't work with Rails 3 and I'm hesitant to use it for mission critical tasks in the app hence the lack of active recent development. So -- what's the best way to deal with these relations. class Author < ActiveRecord::Base has_many :contracts has_many :products,

How to build complex django query as a string

流过昼夜 提交于 2019-12-06 05:00:28
I am dynamically generating a query string with multiple parameters. I am trying to include the object names ('nut', 'jam') in my string. The query has to be an "OR" query. My code is below and I get the error shown below. The solutions here , here , and here did not work for me. from viewer.models import Model1 from django.db.models import Q list1 = [ {'nut' : 'peanut', 'jam' : 'blueberry'}, {'nut' : 'almond', 'jam' : 'strawberry'} ] query_string = "" for x in list1: if len(query_string) == 0: query_string = "Q(nut='%s', jam='%s')" % (x["nut"], x["jam"]) else: query_string = "%s | Q(nut='%s',

How to store functions in django models

爷,独闯天下 提交于 2019-12-06 04:28:53
edit: I completely rewrote the question as the original one didn't clearly explain my question I want to run a function which is specific to each particular model instance. Ideally I want something like this: class MyModel(models.Model): data = models.CharField(max_length=100) perform_unique_action = models.FunctionField() #stores a function specific to this instance x = MyModel(data='originalx', perform_unique_action=func_for_x) x.perform_unique_action() #will do whatever is specified for instance x y = MyModel(data='originaly', perform_unique_action=func_for_y) y.perform_unique_action()

How to use encoder factory in Symfony 2 inside the model setter?

心已入冬 提交于 2019-12-06 02:21:50
问题 This question about Symfony 2.1 How can I encode User password with: $factory = $this->get('security.encoder_factory'); $user = new Acme\UserBundle\Entity\User(); $encoder = $factory->getEncoder($user); $password = $encoder->encodePassword('ryanpass', $user->getSalt()); $user->setPassword($password); And base config: # app/config/security.yml security: # ... encoders: Acme\UserBundle\Entity\User: sha512 Inside the setter models: class User implements UserInterface, \Serializable { public

Yii2 custom validator not working as guide suggests

只愿长相守 提交于 2019-12-06 02:12:45
I've searched around here for some time today, but I'm unable to understand why my validators aren't working in my model. I have a Module with a model "Page" in my code below. I have 2 attributes that I need to use to validate the model. They are hero_link and hero_linked. If hero_linked is true, I want to require hero_link. In the guide here , they explain the proper syntax for this kind of validator I have used this syntax in my model, but it doesn't validate as I'd expect. I've added the whenClient property as well, so I can use client side validation here. my relevant code is below: //

Redirect on catching an exception in a method in the model

你。 提交于 2019-12-06 01:26:58
I am using Authlogic-connect to connect various service providers. There is a method in user.rb def complete_oauth_transaction token = token_class.new(oauth_token_and_secret) old_token = token_class.find_by_key_or_token(token.key, token.token) token = old_token if old_token if has_token?(oauth_provider) self.errors.add(:tokens, "you have already created an account using your #{token_class.service_name} account, so it") else self.access_tokens << token end end When a service provider is already added it gives the error as stated in the has_token? method and the page breaks. I need to redirect

How Do I Use A Tuple Properly in ASP.NET MVC 4

不羁的心 提交于 2019-12-06 00:59:22
I'm trying to use two models in one view using a tuple but I'm getting this error: Server Error in '/' Application. The model item passed into the dictionary is of type 'PagedList.PagedList 1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple 2[PagedList.IPagedList 1[S00117372CA3.Product],System.Collections.Generic.IEnumerable 1[S00117372CA3.Order]]'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception

django 学习之hello world

北城余情 提交于 2019-12-06 00:49:56
使用eclipse +django1.9+python2.7学习环境 在写完models和views之后,需要同步数据库 django1.9之后的不再使用sysdb的命令使用 1.manage.py migrate来初始化数据的基本表 2.使用 manage.py creaetsuperuser创建一个超级用户管理员 3.使用 manage.py makemigrations 模块(model) ,创建对应的表 models的创建 class BlogPost(models.Model): title = models.CharField(max_length = 150) content = models.TextField() timestamp = models.DateTimeField() class BlogPostAdmin(admin.ModelAdmin): list_display = ('title', 'content', 'timestamp') admin.site.register(BlogPost, BlogPostAdmin) views.py from django.template import loader,Context from django.http import HttpResponse from blog.models import

Django RESTFramework——更新数据 (5)

戏子无情 提交于 2019-12-06 00:21:44
完整实例 项目结构 Floral Floral accounts manage.py Floral/urls.py from django.conf.urls import url, include from rest_framework import routers from accounts import views router = routers.DefaultRouter() router.register(r 'users' , views.UserViewSet) router.register(r 'groups' , views.GroupViewSet) router.register(r 'ips' , views.IpViewSet) router.register(r 'comments' , views.CommentViewSet) Floral/accounts/models.py from django.db import models # Create your models here. from django.contrib.auth.models import AbstractBaseUser from rest_framework import serializers from django.utils.translation import

外键查询

若如初见. 提交于 2019-12-05 21:01:47
class AuthorInfo(models.Model): name = models.CharField(max_length=100) gender = models.CharField(max_length=100) def __str__(self): return self.name class Meta: verbose_name = "作者表" verbose_name_plural = verbose_name class BookInfo(models.Model): author = models.ForeignKey(AuthorInfo,related_name="zuozhe",on_delete=models.CASCADE) title = models.CharField(max_length=100) price = models.CharField(max_length=100) def __str__(self): return self.title class Meta: verbose_name = "图书表" verbose_name_plural = verbose_name from .models import AuthorInfo,BookInfoadmin.site.register(AuthorInfo)admin