You can use default option while creating model

Sent from my iPhone

> On 7 May 2020, at 8:53, Derek <[email protected]> wrote:
> 
> 
> Its unclear what you mean by "last ID" - and I cannot see the value of 
> storing such as they are available in the database...
> 
> But have a look at 
> https://stackoverflow.com/questions/12649659/how-to-set-a-django-model-fields-default-value-to-a-function-call-callable-e
>  
> 
> 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 <[email protected]> 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, <[email protected]> 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 [email protected].
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/django-users/5f810453-c7ca-4c10-bca1-4cfe11ea496a%40googlegroups.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 [email protected].
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/CAHV4E-dDRD%3DLteuDo7KuATzjKem-ThvtozzHuaqkwAeAC%2BBrZw%40mail.gmail.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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/48c4d59e-8241-4739-b8df-feab45a7fa3a%40googlegroups.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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/B80CE7F3-311A-4F57-8DB0-9B80997DEAC4%40gmail.com.

Reply via email to