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:
- How to erase or don't allow users to add data in the wrong fields?
- 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()))