小编Jef*_* Mc的帖子

如何仅在django中将外键选择限制为相关对象

我有一个类似于以下的双向外国关系

class Parent(models.Model):
  name = models.CharField(max_length=255)
  favoritechild = models.ForeignKey("Child", blank=True, null=True)

class Child(models.Model):
  name = models.CharField(max_length=255)
  myparent = models.ForeignKey(Parent)
Run Code Online (Sandbox Code Playgroud)

如何将Parent.favoritechild的选择仅限于父母本身的子女?我试过了

class Parent(models.Model):
  name = models.CharField(max_length=255)
  favoritechild = models.ForeignKey("Child", blank=True, null=True, limit_choices_to = {"myparent": "self"})
Run Code Online (Sandbox Code Playgroud)

但这会导致管理界面不列出任何子项.

python django django-models

51
推荐指数
5
解决办法
4万
查看次数

在NHibernate中规范化EnumStringType

我目前在NHibernate中使用枚举如下映射..

public enum UploadMethod
{
    Java, Silverlight, Gears, Flash
}

class UploadMethodType : EnumStringType
{
    public UploadMethodType() : base(typeof(UploadMethod), 255) { }
}

public class Person
{
    /* Bunch of non interesting properties... */
    public UploadMethod PreferredUploadMethod { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 <class name="Person" lazy="false" table="[dbo].[People]">
  <!-- Bunch of non interesting properties... -->
  <property name="PreferredUploadMethod" type="UploadMethodType" />
 </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

除了数据库远离规范化之外,其中的工作完全正常,People表中的每一行都有一个包含四个文本字符串之一的列.我现在将它分成一个新表,但我不确定如何在NHibernate中创建映射.我的第一直觉只是<many-to-one>但我不希望这个枚举成为它自己的实体,我基本上只需要NHibernate来做一个简单的连接来增加额外的列.

作为一个止损,我刚刚创建了一个视图,它工作得很好,但我宁愿那个抽象层在我的NHibernate映射中,而不是模式.

c# nhibernate nhibernate-mapping

3
推荐指数
1
解决办法
2166
查看次数