👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m working on a multi-tenanted application in which some users can define their own data fields (via the admin) to collect additional data in forms and report on the data. The latter bit makes JSONField not a great option, so instead I have the following solution:
class CustomDataField(models.Model):
"""
Abstract specification for arbitrary data fields.
Not used for holding data itself, but metadata about the fields.
"""
site = models.ForeignKey(Site, default=settings.SITE_ID)
name = models.CharField(max_length=64)
class Meta:
abstract = True
class CustomDataValue(models.Model):
"""
Abstract specification for arbitrary data.
"""
value = models.CharField(max_length=1024)
class Meta:
abstract = True
Note how CustomDataField has a ForeignKey to Site - each Site will have a different set of custom data fields, but use the same database. Then the various concrete data fields can be defined as:
class UserCustomDataField(CustomDataField):
pass
class UserCustomDataValue(CustomDataValue):
custom_field = models.ForeignKey(UserCustomDataField)
user = models.ForeignKey(User, related_name="custom_data")
class Meta:
unique_together=(("user","custom_field"),)
This leads to the following use:
custom_field = UserCustomDataField.objects.create(name="zodiac", site=my_site) #probably created in the admin
user = User.objects.create(username="foo")
user_sign = UserCustomDataValue(custom_field=custom_field, user=user, data="Libra")
user.custom_data.add(user_sign) #actually, what does this even do?
But this feels very clunky, particularly with the need to manually create the related data and associate it with the concrete model. Is there a better approach?
Options that have been pre-emptively discarded:
- Custom SQL to modify tables on-the-fly. Partly because this won"t scale and partly because it"s too much of a hack.
- Schema-less solutions like NoSQL. I have nothing against them, but they"re still not a good fit. Ultimately this data is typed, and the possibility exists of using a third-party reporting application.
- JSONField, as listed above, as it"s not going to work well with queries.
👻 Read also: what is the best laptop for engineering students?
Django dynamic model fields __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Django dynamic model fields __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Django dynamic model fields, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Django dynamic model fields
- Deutsch Django dynamic model fields
- Français Django dynamic model fields
- Español Django dynamic model fields
- Türk Django dynamic model fields
- Русский Django dynamic model fields
- Português Django dynamic model fields
- Polski Django dynamic model fields
- Nederlandse Django dynamic model fields
- 中文 Django dynamic model fields
- 한국어 Django dynamic model fields
- 日本語 Django dynamic model fields
- हिन्दी Django dynamic model fields
Vigrinia | 2023-03-24
__del__ is always a bit confusing 😭 Django dynamic model fields is not the only problem I encountered. I am just not quite sure it is the best method
Rome | 2023-03-24
I was preparing for my coding interview, thanks for clarifying this - Django dynamic model fields in Python is not the simplest one. Will get back tomorrow with feedback
Berlin | 2023-03-24
iat is always a bit confusing 😭 Django dynamic model fields is not the only problem I encountered. Will get back tomorrow with feedback