求助:从Admin添加JSON变量时遇'update_grid'对象无'title'属性错误
Hey there! Let's break down why you're seeing this 'update_grid' object has no attribute 'title' error when adding JSON variables via the Django Admin, and walk through how to fix it step by step.
1. First, Audit Your update_grid Model Definition
This error usually pops up because your update_grid model doesn't have a title field, but some part of your code (Admin or views) is trying to access that attribute.
Check your models.py — if it looks something like this (no title field), that's the problem:
# models.py from django.db import models class update_grid(models.Model): json_data = models.JSONField() # No 'title' field defined here # ... other existing fields
Fix Options:
- Add a dedicated
titlefield if you want to store it separately from the JSON:class update_grid(models.Model): title = models.CharField(max_length=255) json_data = models.JSONField() - Add a property to extract
titlefrom your JSON data if the title lives inside the JSON blob:class update_grid(models.Model): json_data = models.JSONField() @property def title(self): # Safely get the title from JSON (handles missing keys) return self.json_data.get('title', 'Untitled')
2. Check Your Django Admin Configuration
If your model does have a title field (or you added the property above), check your admin.py to make sure you're not referencing title in places where it can't be accessed.
For example, if your Admin class looks like this but the model lacks a title field, it'll throw the error:
# admin.py from django.contrib import admin from .models import update_grid class update_gridAdmin(admin.ModelAdmin): list_display = ('id', 'title') # 'title' is referenced here but doesn't exist on the model admin.site.register(update_grid, update_gridAdmin)
Fix:
Either update list_display to use fields/properties that exist on your model, or add the title field/property as mentioned earlier.
3. Verify How JSON Data is Processed in Save Logic
If you have custom save logic in your views or Admin (like overriding save_model), make sure you're not trying to assign a title attribute to the update_grid object when it doesn't exist.
For example, this code would break if the model has no title field:
# In admin.py or views.py def save_model(self, request, obj, form, change): # Trying to assign to obj.title, but the model doesn't have this field obj.title = form.cleaned_data['json_data']['title'] super().save_model(request, obj, form, change)
Fix:
- If you want to store the title separately, add the
titlefield to the model first. - If you want to keep the title inside the JSON, remove the line assigning
obj.titleand ensure your JSON structure includes thetitlekey (like the example you mentioned).
4. Test with a Minimal Setup
To isolate the issue, try a simplified version of your model and Admin first:
# models.py class update_grid(models.Model): title = models.CharField(max_length=255) json_data = models.JSONField() def __str__(self): return self.title # admin.py class update_gridAdmin(admin.ModelAdmin): list_display = ('id', 'title') fields = ('title', 'json_data') admin.site.register(update_grid, update_gridAdmin)
Add an entry via the Admin with your JSON data — if this works, you can gradually reintroduce your custom logic while checking for where the title attribute is being incorrectly referenced.
内容的提问来源于stack exchange,提问作者Rookie_123

