如何清除字段或不允许在django模块中的错误字段中添加内容? [英] How to erase fields or don't allow to add content in the wrong fields in a django module?

查看:169
本文介绍了如何清除字段或不允许在django模块中的错误字段中添加内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢高级,查看和回答我的问题,



图片:



该问题:
https://drive.google。 com / file / d / 0Bw4K4uUse4DYWHZmYW9kZWJtcE0 / edit?usp = sharing



thePosibleSolution:(这是我想要做的照片)image
https://drive.google.com/file/d/0Bw4K4uUse4DYcjZ1TnBVdUk4R3M/编辑?usp =共享



背景:



在我的django模块我有3个表,电影,元数据和一个名为MoviesMetadata的连接表。电影商店电影标题(战斗俱乐部),元数据存储元数据名称(日期,导演,演员,收入等)和元数据的类型(文本,日期,用户,小数,整数)。最后,根据元数据的类型(textValue,integerValue,usersValue,decimalValue等),连接表与Movie MetaTas和Movie的元数据值相连。



问题:



问题是当有人向电影(战斗俱乐部)添加元数据(Date)时,预期的结果是dateValue字段有一些内容(1999/10/15),但用户也可以添加textValue中的内容或不是该元数据类型的其他值类型。最后,元数据 Date 对于电影 战斗俱乐部 可以是 1999/10/15 / strong>(dateValue字段), admin (usersValue字段), 36,000,000.00 (decimalValue字段)等。



问题:


  1. 如何清除或不允许用户在错误的字段中添加数据?

  2. 在模板(post form)中,如何仅显示电影字段,元数据字段和正确的值字段?

代码

  class Movies(models.Model):
name = models.CharField(max_length = 100)
metadata = models.ManyToManyField(元数据,via ='MoviesMetadata')

def __str __(self):
return str(self.name)


class MoviesMetadata(models.Model ):
mo vie = models.ForeignKey(Movies)
metadata = models.ForeignKey(元数据)
textValue = models.CharField(max_length = 100,blank = True,null = True)
dateValue = models。 DateField(auto_now = False,auto_now_add = False,blank = True,null = True)
userValue = models.ForeignKey(User,blank = True,null = True)
decimalValue = models.DecimalField(max_digits = 18,decimal_places = 2,blank = True,null = True)

def _value(self):
''
@property仅返回所选字段类型的值,
忽略其他字段类型的值。
'''
如果self.metadata.fieldtype =='0':
返回self.textValue
elif self.metadata.fieldtype =='1':
return self.dateValue
elif self.metadata.fieldtype =='2':
return self.userValue
else:
return self.decimalValue
value = property (_value)

def __str __(self):
return('%s:%s'%(self.metadata,self.value))


class元数据(models.Model):
name = models.CharField(max_length = 100)
METADATA_FIELD_TYPE =(
('0','Text Value'),
('1','日期值'),
('2','用户价值'),
('3','小数值'),

fieldtype = models.CharField(max_length = 1,choices = METADATA_FIELD_TYPE,default ='0')

def __str __(self):
return('%s(%s)'% (self.name,self.get_field type_display())


解决方案

模式EAV。您将(试图)将一部分DBMS构建到程序+数据库中。 DBMS已经存在来管理数据和元数据。使用它。



没有metatdata的类/表。 只有电影的属性是电影的字段/列。



您的元数据类/表也受到这种影响。没有一个表示行的列表的字段/列。只要列表元素名称就是元数据字段/列名。除了上述之外,您不应该具有 class / table元数据。因为每个这样的行都是对电影的属性进行编码。电影的特定字段/列应该只是电影。



从上面链接的答案之一:


需要使用EAV所以每个实体类型都可以使用自定义字段扩展的
的概念是错误的。只需通过调用
实现更新元数据表,而不是仅更新常规
表:DDL而不是DML。



Thanks in advanced, for viewing and answering my question,

Images:

theProblem: https://drive.google.com/file/d/0Bw4K4uUse4DYWHZmYW9kZWJtcE0/edit?usp=sharing

thePosibleSolution: (this is a photoshoped image of what i want to do) https://drive.google.com/file/d/0Bw4K4uUse4DYcjZ1TnBVdUk4R3M/edit?usp=sharing

Background:

In my django module i have 3 tables, Movies, Metadata and a junction table called MoviesMetadata. Movies stores movies titles (The Fight Club), Metadata stores metadata names (Date, Director, Actors, Revenue, etc.) and the type of that Metadata (text, date, users, decimal, integer). Finally, the junction table joins a Movie with Multiple Metadatas and the Values for that Movie's Metadata depending of the Metadata's type (textValue, integerValue, usersValue, decimalValue, etc.).

Problem:

The problem is that when somebody add a Metadata (Date) to a Movie (The Fight Club) the expected result is that the dateValue field have some content (1999/10/15) but also the user can add content in the textValue or another value type that is not the type of that Metadata. At the end, the values for the Metadata Date for the movie The Fight Club can be 1999/10/15 (dateValue field), admin (usersValue field), 36,000,000.00 (decimalValue field), etc.

Question:

  1. How to erase or don't allow users to add data in the wrong fields?
  2. In the template (post form), How to show only the Movie field, the Metadata field and the correct value field?

Code:

class Movies(models.Model):
    name = models.CharField(max_length=100)
    metadata = models.ManyToManyField(Metadata, through='MoviesMetadata')

    def __str__(self):
        return str(self.name)


class MoviesMetadata(models.Model):
    movie = models.ForeignKey(Movies)
    metadata = models.ForeignKey(Metadata)
    textValue = models.CharField(max_length=100, blank=True, null=True)
    dateValue = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True)
    userValue = models.ForeignKey(User, blank=True, null=True)
    decimalValue = models.DecimalField(max_digits=18, decimal_places=2, blank=True, null=True)

    def _value(self):
        '''
        @property returns only the values from the selected fieldtype,
        ignore the other fieldtypes values.
        '''
        if self.metadata.fieldtype == '0':
            return self.textValue
        elif self.metadata.fieldtype == '1':
            return self.dateValue
        elif self.metadata.fieldtype == '2':
            return self.userValue
        else:
            return self.decimalValue
    value = property(_value)

    def __str__(self):
        return ('%s: %s' % (self.metadata, self.value))


class Metadata(models.Model):
    name = models.CharField(max_length=100)
    METADATA_FIELD_TYPE = (
        ('0', 'Text Value'),
        ('1', 'Date Value'),
        ('2', 'User Value'),
        ('3', 'Decimal Value'),
    )
    fieldtype = models.CharField(max_length=1, choices=METADATA_FIELD_TYPE, default='0')

    def __str__(self):
        return ('%s (%s)' % (self.name, self.get_fieldtype_display()))

解决方案

You are using a DBMS anti-pattern EAV. You are (trying to) build part of a DBMS into your program + database. The DBMS already exists to manage data and metadata. Use it.

Do not have a class/table of metatdata. Just have attributes of movies be fields/columns of Movies.

Your Metadata class/table suffers from this too in an additional way. Do not have a field/column that is a list that represents a row. Just have the list element names be Metadata field/column names. Except that per above you shouldn't have class/table Metadata. Because each such row is encoding the attributes of a movie. The particular fields/columns for movies should just be in Movies.

From one of the answers linked above:

The notion that one needs to use EAV "so every entity type can be extended with custom fields" is mistaken. Just implement via calls that update metadata tables sometimes instead of just updating regular tables: DDL instead of DML.

这篇关于如何清除字段或不允许在django模块中的错误字段中添加内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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