Django Model

已查看 39 次
跳至第一个未读帖子

Максим С

未读,
2018年5月30日 10:27:532018/5/30
收件人 Django users
Hello all.

I'm beginer. Sorry. Can anyone help me with this model. It did't work(TypeError: expected string or bytes-like object

):

import whois

class Domains(models.Model):
domainname = models.CharField(max_length=200, blank=True)
domainuser = models.ForeignKey(User, on_delete=models.CASCADE)
details = whois.whois(domainname)
w = str(details.expiration_date)
domainwhois = models.DateField(default=w)

def __str__(self):
return self.domainname

Domains.save()

Julio Biason

未读,
2018年5月30日 10:37:002018/5/30
收件人 django...@googlegroups.com
Hi,

If I got this right, what you want to do is save the "domainwhois" with the expiration date of the result of the whois.whois function.

Unfortunately, things don't work in class level like this, because things are static here: whois.whois will be run one single time, not on every object. For that, you'll need to change something in the instance. Fortunately, Django allows access to things in the instance with ``save``:

def save(self, *args, **kwargs):
   details = whois.whois(self.domainname)
   self.domainwhois = details.expiration_date
   super().save(*args, **kwargs)

("details" and "w" can be removed from the model itself, as they are variables used only for retriving this information). Have a look how models work here: https://docs.djangoproject.com/en/2.0/topics/db/models/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53525a55-0d68-4a8c-9031-5d7c3baa7064%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

Sadialiou Diallo

未读,
2018年5月30日 13:06:502018/5/30
收件人 Django users



import whois

class Domains(models.Model):
domainname = models.CharField(max_length=200, blank=True)
domainuser = models.ForeignKey(User, on_delete=models.CASCADE)
details = whois.whois(domainname)
w = str(details.expiration_date)
domainwhois = models.DateField(default=w)

def __str__(self):
return self.domainname

Domains.save()


after you definded the save method 


domaine=Domaine()
domaine.domainename=.....
domaine.domaineuser=User.objects.last()
domaine.details=.......

domaine.save()

Максим С

未读,
2018年5月31日 01:57:462018/5/31
收件人 Django users
Thank you very mutch! It work.

class Domains(models.Model):
domainname = models.CharField(max_length=200, blank=True)
domainuser = models.ForeignKey(User, on_delete=models.CASCADE)
    domainwhois = models.DateField(blank=True, null=True)


def save(self, *args, **kwargs):
details = whois.whois(self.domainname)
self.domainwhois = details.expiration_date
super().save(*args, **kwargs)

среда, 30 мая 2018 г., 17:37:00 UTC+3 пользователь Julio Biason написал:
Hi,

If I got this right, what you want to do is save the "domainwhois" with the expiration date of the result of the whois.whois function.

Unfortunately, things don't work in class level like this, because things are static here: whois.whois will be run one single time, not on every object. For that, you'll need to change something in the instance. Fortunately, Django allows access to things in the instance with ``save``:

def save(self, *args, **kwargs):
   details = whois.whois(self.domainname)
   self.domainwhois = details.expiration_date
   super().save(*args, **kwargs)

("details" and "w" can be removed from the model itself, as they are variables used only for retriving this information). Have a look how models work here: https://docs.djangoproject.com/en/2.0/topics/db/models/
On Wed, May 30, 2018 at 11:12 AM, Максим С <seos...@gmail.com> wrote:
Hello all.

I'm beginer. Sorry. Can anyone help me with this model. It did't work(TypeError: expected string or bytes-like object

):

import whois

class Domains(models.Model):
domainname = models.CharField(max_length=200, blank=True)
domainuser = models.ForeignKey(User, on_delete=models.CASCADE)
details = whois.whois(domainname)
w = str(details.expiration_date)
domainwhois = models.DateField(default=w)

def __str__(self):
return self.domainname

Domains.save()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
回复全部
回复作者
转发
0 个新帖子