Create a function which fill automatically a field in my models

33 views
Skip to first unread message

Anselme SERI

unread,
May 6, 2020, 12:52:46 PM5/6/20
to Django users

Hi,

I use Django 3.0 and I would like to create a function which fill automatically a field in my models.
Can I do it directly in models.py ? How?

Thanks a lot

Motaz Hejaze

unread,
May 6, 2020, 2:45:10 PM5/6/20
to Django users
Yes you can make a normal charfield and assign its default attribute to a function you declare

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5f810453-c7ca-4c10-bca1-4cfe11ea496a%40googlegroups.com.

Anselme SERI

unread,
May 6, 2020, 8:23:22 PM5/6/20
to django...@googlegroups.com
Thanks you for your answer. I know how to make a default value in charfield but I don't know  how to build function which returns value for my default field. In my case this function must retrieve last id on my model and must concataned it with a string and the final result must be affected to default charfield. Have you an example ? Help me plz.

Derek

unread,
May 7, 2020, 9:45:54 AM5/7/20
to Django users
Its unclear what you mean by "last ID" - and I cannot see the value of storing such as they are available in the database...


The example is:

from datetime import datetime, timedelta

# default to 1 day from now
def get_default_my_date():
    return datetime.now() + timedelta(days=1)

class MyModel(models.Model):
    my_date = models.DateTimeField(default=get_default_my_date)


Instead of the date arithmetic, you'd need to write code to, for example, extract last ID from the database (assuming you have an auto-increment ID set) and do the concatenation.




On Thursday, 7 May 2020 02:23:22 UTC+2, Anselme SERI wrote:
Thanks you for your answer. I know how to make a default value in charfield but I don't know  how to build function which returns value for my default field. In my case this function must retrieve last id on my model and must concataned it with a string and the final result must be affected to default charfield. Have you an example ? Help me plz.

Le mer. 6 mai 2020 à 18:44, Motaz Hejaze <trap...@gmail.com> a écrit :
Yes you can make a normal charfield and assign its default attribute to a function you declare

On Wed, 6 May 2020, 6:53 pm Anselme SERI, <anselme...@gmail.com> wrote:

Hi,

I use Django 3.0 and I would like to create a function which fill automatically a field in my models.
Can I do it directly in models.py ? How?

Thanks a lot

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Phako Perez

unread,
May 7, 2020, 3:17:05 PM5/7/20
to django...@googlegroups.com
You can use default option while creating model

Sent from my iPhone

On 7 May 2020, at 8:53, Derek <game...@gmail.com> wrote:


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/48c4d59e-8241-4739-b8df-feab45a7fa3a%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages