Hello,
I read the part of the documentation that talks about how to create a custom template field but the example used is not explicit enough. In fact the field I want to create must be an object with two attributes. The first of type float() and the second would be a date defined automaticallyby timezone.now(). So the idea is that I can easily query the date attribute of the object (which will be nothing more than a model field) so that it returns me the precise moment at which a data of the type of object in question would be assigned to that field in the model. Basically I would like to do something like this:
class MyModel(models.Model):
myField = CustomField(''' set some options here ''')
example = MyModel.objects.create(myField=120000)
value = example.myField.value # which will be the first attribute of my object field
date = example.myField.date # the second attribute
First, your example isn’t matching the description of your text.
You’ve identified a Model named MyModel - which is your representation of your data in a database. However, you’re talking about templates and form fields which are something completely different.
Your model would be more appropriately defined as something like:
class MyModel(models.Model):
value = models.CharField(...)
date = models.DateField(...)
You can then define a form with both of those field for allowing that data to be entered from a web page.
Here !!! Precisely what I want is to able to enclose these two fields in a single field and then query the latter so that it provides me with the value of the value field as well as the value of the date field.
There are no such kind of predefined fields in Django that’s why I turned to how to define a custom field.
My problem lies in the fact that I do not know exactly how to go about it since the documentation was not so clear enough on this subject
I’m asking because the way you’re phrasing your question gives me the impression that you don’t have a clear idea of what a Model is, and working your way through either of those tutorials should help clear up that confusion. (And, if you’ve worked through one and it’s still not clear, then I’d suggest working through the other.)
Yes I have read all that and I know very well what a Django model is. I don’t know… maybe I am exaggerating the possibilities of being able to create a custom template field. All I want is to be able to create a custom field to test my idea. But hey, thank you very much for your intervention, I just have to find another idea.
thank you so much…
You say you understand what a Model is, yet you express the idea of combining fields such that you can find one from the other - when that’s precisely the purpose of what a Model is and does.
So, either you really don’t understand what a model is - or you don’t understand what a custom template field is - but either way, it’s clear that you don’t realize yet that you’re conflating two completely separate topics.
If you want to take a step back and present what it is you’re really trying to accomplish, instead of starting from how you’re trying to do it, we might be able to lead you in the right direction.