English 中文(简体)
无法指定“ [u 192.168.99.1, u 192.1689.99.1] ” : “ 多边广域网. nameserver ” 必须是一个“ Nameserver” 实例 。
原标题:Cannot assign "[u 192.168.99.1 , u 192.168.99.1 ]": "MultiWAN.nameserver" must be a "NameServer" instance

考虑以下搭配模式:

class MultiWAN(models.Model):

    isp_name = models.CharField(max_length=10)
    description  = models.TextField(null=True)
    ip_address   = models.IPAddressField(null=True)
    subnet       = models.IPAddressField(null=True)
    gateway      = models.IPAddressField(null=True)
    nameserver   = models.ForeignKey( NameServer )
    weight       = models.IntegerField(null=False)
    interface    = models.CharField(max_length=5) 



class NameServer(models.Model):
    """  A Isp can have more than one nameserver so far we are declearing a seperate table 
    """         
    name = models.IPAddressField(null=False)    

我已写下相应的视图, 以将 User 输入并保存到 db 。

def multiwan_info_save(request):

  #  try:
        isp_nm = request.POST[ ispname_mw ]
        desc_mw  = request.POST[ desc_mw ]
        ip_address_mw  = request.POST[ ip_address_mw ]
        subnet_mw = request.POST[ subnet_mw ]
        nameserver_mw = request.POST.getlist( nameserver_mw )
       # nameserver2_mw = request.POST[ nameserver2_mw ]
        gateway_mw = request.POST[ gateway_mw ]
        weight_mw = request.POST[ weight_mw ]
        interface_mw = request.POST[ interface_mw ]
        print "+++++++++++++++================="
        print nameserver_mw
        nmservr = NameServer()
        mv = MultiWAN(isp_name=isp_nm,description=desc_mw,ip_address=ip_address_mw,subnet = subnet_mw,gateway=gateway_mw,weight=weight_mw,interface = interface_mw,nameserver = nameserver_mw)

        mv.save()
    #except:

请帮助我哪里出错。

问题回答

您将多 WAN. nameserver 定义为名称服务器模型上的外国密钥, 所以您需要将一个单一的名称服务器实例传递给多 WAN, 即 :

nameserver = NameServer.objects.create(name="XXX.XXX.XXX.XXX")
mv = MultiWan.objects.create(nameserver=nameserver, ....)

现在,鉴于您正在通过IP地址列表, 我猜您误解了多广域网/ 名服务员的关系 - 看起来您想要名服务员在多广域网上有一个外国密钥 。

作为附加说明:您应该真正使用表格(或模型格式)代替,因为它可以净化大部分投入并处理验证工作。

正如Bruno所说, 您必须通过一个名称服务器对象, 因为您宣布域为 ForeignKeys, 但也考虑在之前检查是否存在名称服务器 。 一个很好的做法是使用 < a href=" https://docs. djangoproject. com/ en/ dev/ ref/ models/querysets/ # get- or- creates" rel= “ nofol” >objects. get_ or_ capement 而不是创建 。

nameserver, is_new = NameServer.objects.get_or_create(name= ... )

这样您才能接收 namesserver 实例, 新的将会是 True 或 False, 取决于对象是否创建 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签