Wednesday, May 30, 2018

Re: Django Model

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.
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

--
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-users@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/d77412bc-9085-4753-815c-8f5527ff412b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment