1

Django で URL 短縮アプリケーションを作成しています。URL の有効期限が切れているかどうかを確認したい場合 (URL は 3 時間のみ有効です)。以下は私のmodels.pyですが、動作していません。エラーが発生していますcan't subtract datetime object

import uuid
import base64
import datetime
from django.db import models
from django.contrib import admin
#------------------------------------------------------------------------------ 

def _generateShortUrl():
    """
        This function will generate base64 encoded id
    """
    return base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]


class link(models.Model):
    """
        This is a link class 
    """
    link = models.URLField()                                    # To store user entered URL
    hits = models.IntegerField(default=0)                       # How many hits for particular URL
    last_used = models.DateTimeField(auto_now=True)             # When URL is last used 
    short_url = models.CharField(max_length=6,unique=True)      # base64 URL encoded id  

    def linkValidate(self):
        timediff = datetime.datetime.now() - self.last_used                       
        return timediff.min

    def save(self, *args, **kwargs):
        """
            Custom Save method for link model 
        """
        self.short_url = _generateShortUrl()        
        super(link, self).save(*args, **kwargs)   


class user_info(models.Model):
    """
        This is a user_info class 
    """
    user_agent = models.TextField()                 # Stores user_agent name used by user
    user_ip = models.ManyToManyField(link)          # Stores user's IP

    def userIP(self):
        """Method to return tags related to store"""
        return ','.join([t.link for t in self.link.all()])

#------------------------------------------------------------------------------ 

class linkAdmin(admin.ModelAdmin):
    """
        link admin class
    """
    list_display = ('link','hits','short_url','last_used',
                    'linkValidate')
    ordering = ('hits',)

class userInfoAdmin(admin.ModelAdmin):
    """
        user_info admin class
    """
    list_display = ('user_agent','userIP')

#------------------------------------------------------------------------------ 
admin.site.register(link,linkAdmin)
admin.site.register(user_info,userInfoAdmin)

誰か助けてください。

4

1 に答える 1