在Django中,当你有一个父类和多个从它继承的子类时,你通常会通过parentclass.childclass1_set或parentclass.childclass2_set访问一个子节点,但如果我不知道我想要的特定子类的名称呢?
有没有办法在不知道子类名的情况下获取parent-> child方向的相关对象?
我试图通过多对多关系过滤一堆对象.因为trigger_roles字段可能包含多个条目,所以我尝试了包含过滤器.但是因为它被设计为与字符串一起使用,我几乎无能为力地过滤这种关系(你可以忽略values_list()atm.).
此功能附加到用户配置文件:
def getVisiblePackages(self):
visiblePackages = {}
for product in self.products.all():
moduleDict = {}
for module in product.module_set.all():
pkgList = []
involvedStatus = module.workflow_set.filter(trigger_roles__contains=self.role.id,allowed=True).values_list('current_state', flat=True)
Run Code Online (Sandbox Code Playgroud)
我的工作流模型看起来像这样(简化):
class Workflow(models.Model):
module = models.ForeignKey(Module)
current_state = models.ForeignKey(Status)
next_state = models.ForeignKey(Status)
allowed = models.BooleanField(default=False)
involved_roles = models.ManyToManyField(Role, blank=True, null=True)
trigger_roles = models.ManyToManyField(Role, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
虽然解决方案可能很简单,但我的大脑不会告诉我.
谢谢你的帮助.
我相信标题是不言自明的.如何在PostgreSQL中创建表结构以建立多对多关系.
我的例子:
Product(name, price);
Bill(name, date, Products);
Run Code Online (Sandbox Code Playgroud) 我想在其中存储一些额外的信息,自动创建,ManyToMany join-table.我怎么在Django那样做?
在我的情况下,我有两个表:"员工"和"项目".我想要存储的是每个员工每个项目每小时工作收到多少,因为这些值不相同.那么,我该怎么做?
我遇到的是,而不是方法"ManyToManyField",显式创建第三个类/表来存储这些新信息,并使用"ForeignKey"方法设置与"Employees"和"Projects"的关系.我很确定它会起作用,但这是最好的方法吗?
从我在网上和编程实体框架CodeFirst一书中看到的例子中,当你在两个类上都有一个集合时,EF将创建一个映射表,例如MembersRecipes,每个类的主键将链接到该表.
但是,当我执行下面的操作时,我会在Recipes表中调用一个新字段,Member_Id并Recipe_Id在Members表中创建一个字段.
这只会创建两个一对多关系,但不会产生多对多关系,所以我可以将成员3链接到食谱(4,5,6)和配方4链接到成员(1,2,3)等.
有没有办法创建这个映射表?如果是这样,你如何命名其他东西,如"烹饪书"?
谢谢
public abstract class Entity {
[Required]
public int Id { get; set; }
}
public class Member : Entity {
[Required]
public string Name { get; set; }
public virtual IList<Recipe> Recipes { get; set; }
}
public class Recipe : Entity {
[Required]
public string Name { get; set; }
[ForeignKey("Author")]
public int AuthorId { get; set; }
public virtual Member Author { get; …Run Code Online (Sandbox Code Playgroud) 我有两张桌子/收藏品; 用户和组.用户可以是任意数量的组的成员,用户也可以是任意数量的组的所有者.在关系数据库中,我可能有第三个名为UserGroups的表,其中包含UserID列,GroupID列和IsOwner列.
我正在使用MongoDB,我确信在文档数据库中存在这种关系的不同方法.我应该将Users表中的groups和groups-as-owner列表作为两个ObjectID数组嵌入吗?我是否还应该将Groups表中的成员和所有者列表存储为两个数组,从而有效地镜像导致重复关系信息的关系?
或者,桥接UserGroups表是文档数据库中用于多对多关系的合法概念?
谢谢
我有两个名为Category和Item的PHP模型类.类别可能包含许多项目,而项目可能属于许多类别.我已经为这两个类创建了ManyToMany关系:
class Category
{
/**
* @ORM\ManyToMany(targetEntity="Item", mappedBy="categories", cascade={"persist"})
*/
private $items;
/**
* Add items
*
* @param Ako\StoreBundle\Entity\Item $items
*/
public function addItems(\Ako\StoreBundle\Entity\Item $items)
{
$this->items[] = $items;
}
/**
* Get items
*
* @return Doctrine\Common\Collections\Collection
*/
public function getItems()
{
return $this->items;
}
}
Run Code Online (Sandbox Code Playgroud)
和:
class Item
{
/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="items", cascade={"persist"})
* @ORM\JoinTable(name="item_category",
* joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
* )
*/
private $categories;
/**
* Add categories
*
* @param Ako\StoreBundle\Entity\Category $categories
*/ …Run Code Online (Sandbox Code Playgroud) 我有一个phone_models,phone_problems和一个phone_model_phone_problem数据透视表.数据透视表有一个额外的列"价格".
PhoneModel:
class PhoneModel extends \Eloquent
{
public function problems()
{
return $this->belongsToMany('RL\Phones\Entities\PhoneProblem')->withPivot('price');
}
}
Run Code Online (Sandbox Code Playgroud)
PhoneProblem:
class PhoneProblem extends \Eloquent
{
public function models()
{
return $this->belongsToMany('PhoneModel')->withPivot('price');
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获得具有特定问题的特定手机的价格.
这就是我现在的方式,但我觉得Laravel有一个内置的Eloquent功能,我找不到这样做的方式更简单:
$model = $this->phoneService->getModelFromSlug($model_slug);
$problem = $this->phoneService->getProblemFromSlug($problem_slug);
Run Code Online (Sandbox Code Playgroud)
所有这一切都是从他们的slug中选择特定的模型和问题.
那么我所做的就是凭借这些凭据我得到的价格是这样的:
$row = DB::table('phone_model_phone_problem')
->where('phone_model_id', '=', $model->id)
->where('phone_problem', '=', $problem->id)
->first();
Run Code Online (Sandbox Code Playgroud)
所以现在我可以得到这样的价格,$row->price但我觉得需要一个更简单,更"Laravel"的方式来做到这一点.
Doctrine中fetch="EAGER"和fetch="LAZY"注释之间有什么区别@ManyToOne?
/**
* @ManyToOne(targetEntity="Cart", cascade={"all"}, fetch="EAGER")
*/
/**
* @ManyToOne(targetEntity="Cart", cascade={"all"}, fetch="LAZY")
*/
Run Code Online (Sandbox Code Playgroud) 我正在尝试打印所有会议的清单,并为每个会议打印3个演讲者.
在我的模板中,我有:
{% if conferences %}
<ul>
{% for conference in conferences %}
<li>{{ conference.date }}</li>
{% for speakers in conference.speakers %}
<li>{{ conference.speakers }}</li>
{% endfor %}
{% endfor %}
</ul>
{% else %}
<p>No Conferences</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
在我的views.py文件中我有:
from django.shortcuts import render_to_response
from youthconf.conference.models import Conference
def manageconf(request):
conferences = Conference.objects.all().order_by('-date')[:5]
return render_to_response('conference/manageconf.html', {'conferences': conferences})
Run Code Online (Sandbox Code Playgroud)
有一个名为会议的模型.它有一个名为Conferences的类,其中有一个名为扬声器的ManyToManyField
我收到错误:
Caught an exception while rendering: 'ManyRelatedManager' object is not iterable
Run Code Online (Sandbox Code Playgroud)
用这一行: {% for …
many-to-many ×10
django ×4
database ×2
doctrine-orm ×2
python ×2
c# ×1
django-orm ×1
laravel ×1
model ×1
mongodb ×1
mysql ×1
orm ×1
php ×1
pivot-table ×1
postgresql ×1
sql ×1
symfony ×1