I would like to set a field in a Django model which should be updated everytime a record of the corresponding table is updated by a ModelForm (any field of the table).
I am wondering which of the following options is the best practices one:
Add the "auto_now" argument to the model field.
Include the 'type': 'hidden' attribute to the corresponding widget in the form where the records will be submitted. If this... how should be included the 'value' attribute for the widget?
I know the auto_now argument updates the field only when Model.save() is called. So the question could be rebuilt as... Is the Model.save() called when a ModelForm (linked to the model) is submitted?
Thank you very much.
auto_now Automatically set the field to now every time the object is saved.
So to save updated record time auto_now is the best option
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
https://docs.djangoproject.com/en/3.1/ref/models/fields/#datefield
Related
I am experimenting with moving our project over to UUID field primary keys. I've created a branch, deleted all the migrations and the database, and am trying to makemigrations when I hit new errors.
Per the docs, I made id an explicit field in our site Abstract Base Class of Model:
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
I was surprised that this results in a new error for ModelForms where 'id' is included in the fields property. The error says:
```django.core.exceptions.FieldError: 'id' cannot be specified for Statement model form as it is a non-editable field```
I removed 'id' from one Form, but for others it seems pretty essential to the function of the form / formsets that the primary key be returned with POST data. The Django implicit 'id' integer autofield is not editable, yet we did not get this error before, and we still don't where fields = '__all__' is set.
First you need to remove the non-editable field from your class form list of fields if relevant.
Then update your model admin to make your id as read_only:
class YourModelAdmin(admin.ModelAdmin):
readonly_fields=('id',)
form = YourModelModelForm # if relevant, otherwise keep the readonly_fields
It seems no matter what I do I cannot get the parent field to not be required. I'm using DRF version 3.2.3 and Django 1.8.4.
Model definition of field:
parent = models.ForeignKey(
"self", verbose_name=_("Parent"), blank=True, null=True,
default=None, related_name='children')
The model also has a unique_together:
unique_together = (('owner', 'parent', 'name',),)
Serializer definition of field:
parent = serializers.HyperlinkedRelatedField(
view_name='category-detail', queryset=Category.objects.all(),
required=False)
I'm writing unittests and the response code is 400 with a text response of:
{'parent': [u'This field is required.']}
The parent field is a ForeignKey back to another row in the same table.
Gals/Guys any ideas how to fix this?
Sometimes a field can be implicitly made required by some other piece of code. One case I encountered is the model-level unique_together constraint, that makes all included fields required on the serializer level. From the doc:
Note: The UniqueTogetherValidation class always imposes an implicit constraint that all the fields it applies to are always
treated as required. Fields with default values are an exception to
this as they always supply a value even when omitted from user input.
I think you will just have to override the serializer save or viewset create/update to set the value to what you want at this point. Another option is to try to remove the UniqueTogetherValidator from the serializer's validators in its __init__. On the other hand I think it is added for a reason.
It is worth mentioning that in admin and anywhere else ModelForm is used, these fields won't be required because ModelForm is another thing entirely and it handles the validation in its own way.
I'm using a formset to update a single field of a large group of model instances. I'd like to display a time stamp showing the time since the field for that instance was last updated. Since this field will usually be updated once a week, I'd rather use a DateField than a DateTimeField for the time stamp. DateField doesn't seem to get updated on save though. When I change the model field to DateTimeField, however, it works as expected. Here's my code.
#Template
<div class = 'last-updated'> {{ form.instance.last_updated|timesince }} </div>
# Models.py
last_updated = models.DateField(auto_now=True)
# Models.py - This version works
last_updated = models.DateTimeField(auto_now=True)
I've found posts saying to override the model's save() method but this seems like a last resort, and the posts I've found saying this are dated from 2011 and earlier, so probably out of date.
Since the docs list DateField and DateTimeField as more or less the same, with the same optional arguments, I'm wondering why they don't seem to update auto_now in the same way.
Edit
It also looks as though when I change the field type to DateField, the value displayed is the time since creation, not the time since update, and it updates the value for every single item in the formset. To clarify, there is NO custom save method for this model.
having used cakephp in the past, one thing (perhaps the only thing?) i liked about it was that it had a "create" and "update" timestamp capability that was lovely - simply put, when you first added an item, the "create" date was set (assuming you named it right - create_date, i think)
Anytime thereafter, if an update was performed, the "update" field was set to the current time.
Does django have this as well? If so, what/how do i name the fields to get it to pick them up?
It is not added to your model built-in in every table. You must add it as field to your model.
class Message(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Message in this case your table's name.
Sure it has!
Check auto_now and auto_now_add in the doc
From the admin panel I want to populate a slug field based on a certain text field
eg.
Title: My Awesome Page
would automaticaly populate
Slug: my_awesome_page
There used to be a prepoulate_from option for the SlugField up to 0.96. It became an admin option after that. See here for reference on that.
Alternatively, you could override the model's save method to calculate the value of the slug field on save().
This question may be helpful, too.
There is also an app called django-autoslug which provides a field type AutoSlugField. Using that, you could have:
class Something(models.Model):
title = models.CharField(max_lenght=200)
slug = AutoSlugField(populate_from='title')
...
This AutoSlugField has many nice features, such as generating a slug so that it is unique either globally of combined with some other field (maybe a category or the year part of a DateTimeField).
See http://pypi.python.org/pypi/django-autoslug for further details.