0

I am learning web develpoment by django. I am coding according to the book django2byexample. I found a problem that the book hasn't include My VScCode printInstance of 'DateTimeField' has no 'year' member Could someone tell me what` the problem?Thank you. Here is the code:

from django.db import models
from django.utils import timezone 
from django.contrib.auth.models import User 
from django.urls import reverse

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager, self).get_queryset().filter(status='published')

class Post(models.Model):
    STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'))
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now())
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
    objects = models.Manager() 
    published = PublishedManager() 

    class Meta:
        ordering = ('-publish',)
    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail',args=[self.publish.year,self.publish.month,self.publish.day,self.slug])
ZHU LIU
  • 71
  • 4
  • Can you give more code? Maybe the whole code of model? – Nikita Davydov Nov 21 '19 at 11:50
  • https://stackoverflow.com/questions/35990313/avoid-pylint-warning-e1101-instance-of-has-no-member-for-class-with-dyn – Mohit Harshan Nov 21 '19 at 12:09
  • and provide the error trace plz – Nikita Davydov Nov 21 '19 at 12:14
  • I dont see a problem with the current code. Perhaps only vscode isssue ? Unless you provide your stack trace when running the project. However i want to point out one issue in your code - try to create 2 different posts with 1 minute difference. and then check your publish value of each post. I guess you will be surprised. – Enthusiast Martin Nov 21 '19 at 12:25
  • @NikitaDavydov I can`t open the link and get traceback , Because this function is used to built the link,and the link hasn`t built up . – ZHU LIU Nov 21 '19 at 12:41

2 Answers2

2

i find the answer. It is the problem of vscode. we can set in this following step: Then in Visual Studio Code goto: User Settings (Ctrl + , or File > Preferences > Settings if available ) Put in the following (please note the curly braces which are required for custom user settings in VSC):

{"python.linting.pylintArgs": [
     "--load-plugins=pylint_django"
],}

Reference:Class has no objects member

ZHU LIU
  • 71
  • 4
0

This is a VSC problem. However, you can suppress the warning for a certain code block

def get_absolute_url(self):
    #pylint: disable=E1101
    return reverse('blog:post_detail',
          args=[
          self.publish.year,
          self.publish.month,
          self.publish.day,
          self.slug])

Here you only disable the E1101 error for the get_absolute_url method as in the second line of the code. If you want to re-enable it, you can write #pylint: enable=E1101 at the end of the block which it has been disabled for.

Nnaobi
  • 399
  • 3
  • 12