Django admin: How to display the field marked as “editable=False” in the model?
Use Readonly Fields. Like so (for django >= 1.2): class MyModelAdmin(admin.ModelAdmin): readonly_fields=(‘first’,)
Use Readonly Fields. Like so (for django >= 1.2): class MyModelAdmin(admin.ModelAdmin): readonly_fields=(‘first’,)
https://docs.djangoproject.com/en/2.2/ref/models/fields/#datetimefield A date and time, represented in Python by a datetime.datetime instance. You can get a string representation of a DateTimeField casting it directly: str(obj) # obj = qs[0][0] ? or qs[0][1] ? You’ll get result like this (in this example I use datetime.datetime.now() since a DateTimeField is represented by datetime.datetime is the same behavior): … Read more
To make a field in a ModelField a hidden field, use a HiddenInput widget. The ModelForm uses a sensible default widget for all the fields, you just need to override it when the object is constructed. class TagStatusForm(forms.ModelForm): class Meta: model = TagStatus widgets = {‘tag’: forms.HiddenInput()}
I just learned how to do this with a single migration! When running makemigrations django should ask you to set a one-off default. Define whatever you can here to keep it happy, and you’ll end up with the migration AddField you mentioned. migrations.AddField( model_name=”series”, name=”updated_as”, field=models.DateTimeField(default=????, auto_now=True), preserve_default=False, ), Change this one operation into 3 … Read more
Summary Django’s proxy models provide the basis for Single Table Inheritance. However, some effort is required to make it work. Skip to the end for a re-usable example. Background Martin Fowler describes Single Table Inheritance (STI) as follows: Single Table Inheritance maps all fields of all classes of an inheritance structure into a single table. … Read more
If it’s really dictionary like arbitrary data you’re looking for you can probably use a two-level setup with one model that’s a container and another model that’s key-value pairs. You’d create an instance of the container, create each of the key-value instances, and associate the set of key-value instances with the container instance. Something like: … Read more
To anyone else that gets this error by following the old documentation, get_profile() has been depreciated in django 1.7. Instead you can just access the userprofile from the user as below.. u_p = user.userprofile where userprofile is the name of your UserProfile class You can also change to use the newer documentation by clicking on … Read more
Below code helped me resolve this issue python manage.py migrate –run-syncdb
I would suggest that you use Model inheritance. Create a base model that contains date and title. Subclass Msg1 and Msg2 off it as described. Do all your queries (to fill a page) using the base model and then switch to the derived type at the last moment. The really great thing about inheritance is … Read more
The django.db.connections is a simple wrapper around DATABASES defined in your settings. The wrapper class is here: django.db.utils#L137-L227 from django.db import connections # Add connection information dynamically.. connections.databases[‘new-alias’] = { … } # Ensure the remaining default connection information is defined. # EDIT: this is actually performed for you in the wrapper class __getitem__ # … Read more