Django:模型中IntegerField的实例将类型赋予' int'

时间:2017-01-14 06:55:20

标签: django django-models django-admin

我试图实现为IntegerField定义的一些函数。

这是代码:

class student(models.Model):
marks=models.IntegerField(blank=True,default=0,null=True)

def __unicode__(self):
    print self.marks.has_default()
    return str(self.marks)

这是我得到的错误:

'int' object has no attribute 'has_default'

然后我实现了这段代码:

class student(models.Model):
marks=models.IntegerField(blank=True,default=0,null=True)

def __unicode__(self):
    print type(self.marks)
    sample=models.IntegerField()
    print(type(sample))
    return str(self.marks)

这是我得到的输出:

<type 'int'>
<class 'django.db.models.fields.IntegerField'>

现在,我不明白为什么会这样。 不应该两者的类型相同,因为它们都是models.IntegerField()

的实例

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,django的元类产生了这种行为,这是大多数时候你想要的:Sub FilterAddress(FilterVal As String) Dim FilterRng As Range Dim VehicleArr As Variant Dim ServiceArr As Variant With Sheets("Sheet1") '<--| reference your "data" sheet With .Range("C1", .Cells(.Rows.Count, "A").End(xlUp)) '<--| reference its columns A:C cells from row 1 down to column A last not empty one .AutoFilter '<--| remove any previuous filter .AutoFilter Field:=1, Criteria1:=FilterVal 'filter referenced range on its 1st column with 'FilterVal' value With .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible) '<--| reference filtered cells skipping header row ServiceArr = GetOrderedUniqueValuesArrayFromRange(Intersect(.Cells, .Columns(2).EntireColumn)) '<--| fill ServiceArr with unique ordered values from 2nd column of referenced range VehicleArr = GetOrderedUniqueValuesArrayFromRange(Intersect(.Cells, .Columns(3).EntireColumn)) '<--| fill VehicleArr with unique ordered values from 3nd column of referenced range End With End With .AutoFilterMode = False '<--| show all rows back End With ' now the "fun" part >> paste to "Sheet2" With Sheets("Sheet2") .Range("A1").value = "ADDRESS" .Range("B4").value = FilterVal .Range("C1").value = "VEHICLE(S) USED" .Range("B12:B17").ClearContents ' clear service contents from previous run .Range("B12").Resize(UBound(ServiceArr) - LBound(ServiceArr) + 1) = WorksheetFunction.Transpose(ServiceArr) .Range("B50:B55").ClearContents ' clear vehicle contents from previous run .Range("B50").Resize(UBound(VehicleArr) - LBound(VehicleArr) + 1) = WorksheetFunction.Transpose(VehicleArr) End With End Sub 应该给你instance.marks的标记!但是,您可以通过_meta API

访问实际的字段实例
int