Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
import uuid
from django.db import models
from django.core import validators
from django.contrib.auth.models import User
from django.db.models.base import Model
from django.db.models.deletion import SET, SET_NULL
from django.db.models.fields.related import ManyToManyField, OneToOneField


class Author(models.Model):

    class Meta:
        verbose_name = 'Автор'
        verbose_name_plural = 'Авторы'
        ordering = ['id']
        unique_together = ('name', 'age')

    TYPES = (
        ('a', 'forein'),
        ('b', 'domestic'),
        ('c', 'other')
    )
    id = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4)
    name = models.CharField(verbose_name='Имя автора', 
    max_length=200,
     validators=[validators.RegexValidator(regex='^.*em$', message='Wrong')])
    age = models.PositiveIntegerField(verbose_name='Возраст автора')
    email = models.EmailField(verbose_name='Почта автора')
    lit_type = models.CharField(max_length=1, verbose_name='Тип литиратуры', choices=TYPES, default='a')
    
    def __str__(self):
        return self.name


class Book(models.Model):

    class Meta:
        verbose_name = 'Книга'
        verbose_name_plural = 'Книги'
        get_latest_by = 'published'

    title = models.CharField(max_length=200)
    description = models.TextField()
    page_num = models.PositiveIntegerField()
    published = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class ExtUser(models.Model):
    desc = models.CharField(max_length=200)
    is_loggen = models.BooleanField(default=True)
    user = OneToOneField(User, on_delete=SET_NULL, null=True)

    def __str__(self):
        return self.desc

class Product(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Store(models.Model):
    
    name = models.CharField(max_length=200) 
    products = ManyToManyField(Product, related_name="Магазин")

    def __str__(self):
        return self.name


What I have tried:

When I try to access the key, I get this error, how can I fix it?


Terminal
<pre>>>> b_1 = Book.objects.get(pk=1)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\models\query.py", line 650, in get
    raise self.model.DoesNotExist(
web_lib.models.Book.DoesNotExist: Book matching query does not exist.
>>> b1 = Book.objects.get(pk=1)  
Traceback (most recent call last):
web_lib.models.Book.DoesNotExist: Book matching query does not exist.
>>>
Posted
Updated 23-Nov-22 5:17am

1 solution

The message is telling you that the database could not find a Book record with a pk of 1. Check the contents of the database tables to see exactly what it does have.
 
Share this answer
 
Comments
Eva Smirnova 23-Nov-22 11:38am    
I have books in the database, I don't understand where else to look?
Richard MacCutchan 23-Nov-22 11:49am    
Try this in the shell:
for book in Books.objects.all():
    print(book.pk)

That should display the ids of all the Book records in the database. I don't know what the names of the other fields are but you could print the title or name alongside the id number.
Eva Smirnova 23-Nov-22 12:15pm    
I found the cause of the error, it turns out I had two identical elements in the database, that's why it didn't work, I wanted to add two books to the author, now everything worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900