Closed Animesh-Ghosh closed 5 years ago
class tutorial(models.Model):
ARTICLE = 'article'
BOOK = 'book'
CHEATSHEET = 'cheatsheet'
COURSE = 'course'
DOCS = 'docs'
VIDEO = 'video'
CATEGORIES = (
(ARTICLE, 'Article'),
(BOOK, 'Book'),
(CHEATSHEET, 'Cheatsheet'),
(COURSE, 'Course'),
(DOCS, 'Documentation'),
(VIDEO, 'Video'),
)
title = models.CharField(max_length=200)
link = models.URLField()
tags = models.ManyToManyField(tag)
category = models.CharField(max_length=20, choices=CATEGORIES)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
The class is getting populated unnecessarily, plus the code is getting repeated
It's abstracting away the category that will be saved in the database. Typing 'article', 'book', 'course' etc. each time you generate a query seems wrong.
By encapsulating the CATEGORIES
tuple inside the tutorial class, I can access it as an attribute.
Opened #25 in its place.
Suggestion: create a forms.py
file for all 2 of the forms?
Found a bug for the first time you search something, the filter option in working but once you search something it gets diappeared
Suggestion: create a forms.py file for all 2 of the forms?
No
Found a bug for the first time you search something, the filter option in working but once you search something it gets diappeared
Fixed in 42f9cb8
Description: Forms have hard-coded options. I couldn't access the field choices inside the app's views so as to pass them to the context of the required templates. This question looked appropriate and thus I followed the thread and arrived here.
According to the latter, we should remove them according to the DRY principle. This will make it easy to add/remove new categories as needed.
It will also make the queries simpler and no actual strings that are specified in the TUTORIAL_CATEGORIES tuple will have to be used again (either in the shell or otherwise).
Proposed Solution: Encapsulate the
TUTORIAL_CATEGORIES
tuple inside thetutorial
model. Like so: From:To:
Alternatives: Leave the code as is. Forms remain difficult to update (atleast the selection box does).