S
S
Stanislav Konovalov2019-03-09 23:19:47
Django
Stanislav Konovalov, 2019-03-09 23:19:47

Django. Cannot resolve keyword 'user_name' into field?

Hello! How to correctly display ads only for the current user?

def dashboard(request):
  listings = Listing.objects.order_by('-list_date').filter(user_name=request.user)

  paginator = Paginator(listings, 6)
  page = request.GET.get('page')
  paged_listings = paginator.get_page(page)

  context = {
    'listings': paged_listings
  }
  return render(request, 'accounts/dashboard.html', context)

Ad Model:
class Listing(models.Model):
  realtor = models.ForeignKey(Realtor, on_delete=models.CASCADE, verbose_name='Риэлтор')
  region = models.CharField(default="Чуйская", max_length=100, verbose_name='Область')
  city = models.CharField(default="Бишкек", max_length=100, verbose_name='Город')
  district = models.CharField(max_length=100, verbose_name='Район')
  title = models.CharField(blank=True, max_length=200, verbose_name='Заголовок')
  address = models.CharField(max_length=200, verbose_name='Адрес')
  description = models.TextField(blank=True, verbose_name='Описание')	
  stage = models.IntegerField(verbose_name='Этаж')
  rooms = models.IntegerField(verbose_name='Количество комнат')	
  garage = models.IntegerField(default=0, verbose_name='Гараж')
  sqmt = models.IntegerField(verbose_name='Площадь')
  price = models.IntegerField(verbose_name='Цена')
  photo_main = models.ImageField(blank=True, upload_to='photos/%Y/%m/%d/', verbose_name='Основное фото')
  photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, verbose_name='Фото 1')
  photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, verbose_name='Фото 2')
  photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, verbose_name='Фото 3')
  photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, verbose_name='Фото 4')
  photo_5 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, verbose_name='Фото 5')
  photo_6 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True, verbose_name='Фото 6')
  is_published = models.BooleanField(default=True, verbose_name='Публично')
  list_date = models.DateTimeField(default=datetime.now, blank=True, verbose_name='Дата публикации')

  def __str__(self):
    return self.title

  class Meta:
    verbose_name = 'Объявление'
    verbose_name_plural = 'Объявления'

Realtor Extended User Model:
class Realtor(models.Model):
  user_name = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='Пользователь', related_name='user_name')
  full_name = models.CharField(max_length=50, verbose_name='Имя')
  photo = models.ImageField(upload_to='photos/%Y/%m/%d/', verbose_name='Фото')
  description = models.TextField(blank=True, verbose_name='Описание')
  phone = models.CharField(max_length=20, verbose_name='Телефон')
  email = models.CharField(max_length=50, verbose_name='Email')
  is_mvp = models.BooleanField(default=False, verbose_name='Реэлтор месяца')
  hire_date = models.DateTimeField(default=datetime.now, blank=True, verbose_name='Дата приёма на работу')

  def __str__(self):
    return self.full_name

  class Meta:
    verbose_name = 'Риэлтор'
    verbose_name_plural = 'Риэлторы'

Thanks in advance)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question