Metaclasses - Simple Django Models
Description:
Django is a famous back-end framework written in Python. It has a vast list of features including the creation of database tables through "models". You can see an example of such model below:
class Person(models.Model):
first_name = models.CharField()
last_name = models.CharField()
Apart from creating a table it can perform validation, generate HTML forms, and so on. This is possible thanks to metaclasses. Normally there are better solutions than using metaclasses, but they can be of great help in creating powerful framework interfaces. This goal of this kata is to learn and understand how such frameworks works.
Your task is to implement a class Model
and classes for its fields to support functionality like in the following example:
class User(Model):
first_name = CharField(max_length=30)
last_name = CharField(max_length=50)
email = EmailField()
is_verified = BooleanField(default=False)
date_joined = DateTimeField(auto_now=True)
age = IntegerField(min_value=5, max_value=120, blank=True)
user1 = User(first_name='Liam', last_name='Smith', email='liam@example.com')
user1.validate()
print(user1.date_joined) # prints date and time when the instance was created
print(user1.is_verified) # prints False (default value)
user1.age = 256
user1.validate() # raises ValidationError - age is out of range
user2 = User()
user2.validate() # raises ValidationError - first three fields are missing and mandatory
The classes which inherit from Model
should:
- support creation of fields using class-attribute syntax
- have a
validate
method which checks whether all fields are valid
The field types you should implement are described below. Each of them also has parameters blank
(default False
), which determines whether None
is allowed as a value, and default
(default None
) which determines the value to be used if nothing was provided at instantiation time of the Model.
CharField
- a string withmin_length
(default0
) andmax_length
(defaultNone
) parameters, both inclusive if definedIntegerField
- an integer withmin_value
(defaultNone
) andmax_value
(defaultNone
) parameters, both inclusive if definedBooleanField
- a booleanDateTimeField
- a datetime with an extra parameterauto_now
(defaultFalse
). Ifauto_now
isTrue
and no default value has been provided, the current datetime should be used automatically at Model instantion time.EmailField
- a string in the format ofaddress@subdomain.domain
whereaddress
,subdomain
, anddomain
are sequences of alphabetical characters withmin_length
(default0
) andmax_length
(defaultNone
) parameters
Each field type should have its own validate
method which checks whether the provided value has the correct type and satisfies the length/value constraints.
Similar Kata:
Stats:
Created | Jan 11, 2015 |
Published | Jan 18, 2015 |
Warriors Trained | 3136 |
Total Skips | 730 |
Total Code Submissions | 12776 |
Total Times Completed | 277 |
Python Completions | 277 |
Total Stars | 249 |
% of votes with a positive feedback rating | 79% of 74 |
Total "Very Satisfied" Votes | 49 |
Total "Somewhat Satisfied" Votes | 19 |
Total "Not Satisfied" Votes | 6 |
Total Rank Assessments | 8 |
Average Assessed Rank | 3 kyu |
Highest Assessed Rank | 2 kyu |
Lowest Assessed Rank | 8 kyu |