When my debugger execute this line of code from .models import Expense,Category
from views.py
file then it shows an error . The error is :
Exception has occurred: ImportError
attempted relative import with no known parent package
for better understanding about the error , see this image :
views.py
and models.py
are in same directory . And also the directory has init.py file .
some of the code of views.py file :
from django.shortcuts import render,redirect
from .models import Expense,Category
from django.contrib import messages
from django.core.paginator import Paginator
import json
from django.http import JsonResponse
def search_expenses(request):
if request.method=='POST':
search_str=json.loads(request.body).get('searchText')
expenses=Expense.objects.filter( amount__istartswith=search_str,owner=request.user)|Expense.objects.filter(date__istartswith=search_str,owner=request.user)|Expense.objects.filter(description__icontains=search_str,owner=request.user)|Expense.objects.filter(category__icontains=search_str,owner=request.user)
data=expenses.values()
return JsonResponse(list(data),safe=False)
And also some of the code of models.py file :
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
class Expense(models.Model):
amount=models.FloatField()
date=models.DateField(default=now)
description=models.TextField()
owner=models.ForeignKey(to=User,on_delete=models.CASCADE)
category=models.CharField(max_length=266)
def __str__(self):
return self.category
class Meta:
ordering:['-date']
class Category(models.Model):
name=models.CharField(max_length=255)
class Meta:
verbose_name_plural='Category'
def __str__(self):
return self.name
How can I solve this issue ?