如何将django ManyToManyField转换为Django-nonrel字段? [英] How to convert django ManyToManyField into Django-nonrel Field?

查看:92
本文介绍了如何将django ManyToManyField转换为Django-nonrel字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中构建了一个应用程序,但是由于我发现谷歌应用程序引擎不支持Django开箱即用(免费,云sql不能被免费使用)。



我决定移动到Django-nonrel,所以有几个需要转换的datebase字段,我不知道如何:

  class Cate(models.Model):
name = models.CharField(max_length = 100)
description = models.TextField()
create_by = models ForeignKey(User)
create_date = models.DateTimeField('cate created date')

def __unicode __(self):
return self.name

class Product(models.Model):
product_name = models.CharField(max_length = 200)
owner = models.ForeignKey(User)

cate = models.ManyToManyField(Cate )

timestamp = models.DateTimeField('product added date')
view = models.IntegerField(default = 0)

def __unicode __(self):
返回自己这里是user_profile模型,它从用户模型


$中扩展b $ b

  class UserProfile(models.Model):
user = models.OneToOneField(User)

cates = models.ManyToManyField(' ShopCate')

Cate模型由管理员创建,UserProfile可以有很多的类, cate可以属于许多用户,与产品相同。



请帮助构建这些模型,也许有一些关于如何使用Django-nonrel的提示



我真的新到数据库

解决方案

有两种方法可以做到这一点。更便宜的版本是使用ListFields

 从djangotoolbox.fields导入ListField 

class UserProfile(models。模型):
user = models.OneToOneField(User)
cates = ListField(models.ForeignKey(shop.Cate))

ListField只是存储一个Cate ids列表。这有一些重要的限制。


  1. 您的实体限制为1MB,因此限制列表中的实体数量。在这种情况下,这仍然是一个相当大的数字,特别是因为您的实体没有其他的东西。


  2. 您可以对cates字段进行数据存储查询如果它被索引。但是,每个实体都有5000个索引的限制。您将使用一个用户属性,因此在这种情况下,您的会员列表将被限制为有5000个条目。我以前没有打过这个,所以我不知道怎么会失败,我想你会写一个例子来写你的实体。


更昂贵的选择是使用中间映射实体。这为您创建/查询映射实体的额外费用提供了无限关系。

  class UserCateMapping(models.Model)
user = models.ForeignKey(UserProfile)
cate = models.ForeignKey(Cate)

在这种情况下,您需要为每个关系创建一个新实体,并在获取实际要使用的实际Cate或UserProfile实体之前查询UserCateMapping实体。它将比第一个选项更昂贵,但您将拥有无限的映射。


I build an app in django, but since I found out that google app engine doesn't support Django out of the box (free,cloud sql can't be used for free right?).

I decided to move to Django-nonrel, so there are few datebase Field that need converting, and I don't know how:

class Cate(models.Model):
    name = models.CharField(max_length = 100)
    description = models.TextField()
    create_by = models.ForeignKey(User)
    create_date = models.DateTimeField('cate created date')

    def __unicode__(self):
        return self.name

class Product(models.Model):
    product_name = models.CharField(max_length = 200)
    owner = models.ForeignKey(User)

    cate = models.ManyToManyField(Cate)

    timestamp = models.DateTimeField('product added date')
    view = models.IntegerField(default = 0)

    def __unicode__(self):
        return self.product_name

here is the user_profile model which extends from user model

 class UserProfile(models.Model):
     user = models.OneToOneField(User)

     cates = models.ManyToManyField('shop.Cate')

the Cate model is created by admin, UserProfile can have many cates, and same cate can belong to many users, same as product.

please help to construct these models and maybe some tips on how to use Django-nonrel

I am really new to database

解决方案

There's two ways to do this. The cheaper version is to use ListFields

from djangotoolbox.fields import ListField

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    cates = ListField(models.ForeignKey(shop.Cate))

The ListField simply stores a list of Cate ids. There's some important limitations to this.

  1. Your entity is limited to 1MB, so this limits the number of entities in your list. In this case, it'll still be a fairly large number, especially since there's nothing else in your entity.

  2. You can do dataastore queries against the cates field if it's indexed. However, each entity has a limit of 5000 indexes. You'll use one for the user attribute, so in this case, your cates list will be limited to have 5000 entries. I haven't hit this before so I don't know how it would fail, I presume you'd get an exception on writing your entity.

The more expensive option is to use an intermediate mapping entity. This gives you unlimited relations for the extra expense of creating/querying the mapping entities.

class UserCateMapping(models.Model)
    user = models.ForeignKey(UserProfile)
    cate = models.ForeignKey(Cate)

In this case, you'll need to create a new entity for each relation, and query for the UserCateMapping entities before fetching the actual Cate or UserProfile entity you actually want to use. It's going to be more costly than the first option, but you'll have unlimited mappings.

这篇关于如何将django ManyToManyField转换为Django-nonrel字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆