kikee1222
kikee1222

Reputation: 1996

Django has no object attribute named text - but I don't expect it to?

I have the below structure,

When I click on the model name in the admin view, I get the below error. What does this mean?

AttributeError at /admin/app/tasksx/
'tasksx' object has no attribute 'text'
Request Method: GET

Admin.py

from django.contrib import admin
from .models import tasksx

admin.site.register(tasksx)

Views.py

def create_task(request):
    if request.method == 'POST':
        creator = request.user
        job_title = 'data engineer'
        skill_name = request.POST.get('skill_name')
        starting = request.POST.get('starting')
        description = request.POST.get('description')
        target_date = request.POST.get('target_date')
    i = tasksx.objects.create(creator=creator, job_title=job_title, skill_name=skill_name,  starting=starting, current=starting, description=description, target_date=target_date)
    messages.success(request, ('Skill created'))
    return redirect('index')

models.py

class tasksx(models.Model):
    job_title = models.CharField(max_length=400, default="data")
    creator = models.CharField(max_length=400, default="none")
    skill_name = models.CharField(max_length=400, default="none")
    starting = models.CharField(max_length=400, default="none")
    current = models.CharField(max_length=400, default="none")
    description = models.CharField(max_length=4000000, default="none")
    target_date = models.DateTimeField(default=datetime.now)
    def __str__(self):
        return self.text

Upvotes: 0

Views: 26

Answers (2)

Paolo
Paolo

Reputation: 853

Expanding my comments to avoid extended discussion:

In your tasksx model's __str__ method you are trying to return self.text when you don't have a text field anywhere in the model.

If you want to display the title, modify the method's return to.

def __str__(self):
    return self.job_title

Now, if you want to see all fields in the admin interface, you would need to modify your app's admin.py.

admin.py

from django.contrib import admin
from .models import tasksx

class Tasksx_Admin(admin.modelAdmin):
    # Add whatever fields you want to display in the admin 
    # in list_diplay tuple.

    list_display = ('job_title', 'creator', 'skill_name', 'starting', 'current', 'description', 'target_date', )

# Register the Taskx_Admin class.

admin.site.register(tasksx, Taskx_Admin)

Upvotes: 1

WilsonPena
WilsonPena

Reputation: 1561

In the tasksx model you defined:

def __str__(self):
    return self.text

But there is no text property in the model.

Upvotes: 1

Related Questions