如何使用Django模型函数 [英] How to use Django model functions

查看:79
本文介绍了如何使用Django模型函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面列出的几个型号的应用程序:

I have an application with a few models listed below:

devices.Device
buildings.Building
buildings.Stack
buildings.Switch
rooms.Room

在我的程序,我接受一个 CSV 文件与数据。然后我将其解析成代表每个模型的 JSON ,并使用 get_or_create()将数据添加到数据库中。以下是一个示例:

In my program, I accept a CSV file with data. Then I parse it into JSON representing each model and use get_or_create() to add the data into the database. Here is an example:

                obj, created = Building.objects.get_or_create(
                    name=entry["fields"]["name"],
                    number=entry["fields"]["number"])
                if created:
                    output += "Building was created!\n"
                else:
                    output += "Building already exists!\n"

上面的工作很好。但是,我尝试下面的代码,不能让它工作:

And the above is working well. However, I tried the code below, and could not get it to work:

                obj, created = Switch.objects.get_or_create(
                    stack=entry["fields"]["stack"],
                    number=entry["fields"]["number"],
                    ip=entry["fields"]["ip"],
                    num_ports=entry["fields"]["num_ports"])
                if created:
                    output += "Switch was created!\n"
                else:
                    output += "Switch already exists!\n"

哪个导致此错误:

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/opt/cutsheets/cutsheets/views.py" in import_data
  156.             output = process_xls_data(temp_dir)
File "/opt/cutsheets/cutsheets/views.py" in process_xls_data
  113.                     obj, created = Switch.objects.get_or_create(

Exception Type: NameError at /import
Exception Value: global name 'Switch' is not defined

我怀疑它与模型的构建方式有关我没有做)任何人都可以建议如何修改上述代码,以便我可以使用 get_or_create()插入交换机和堆栈。

I suspect that it has to do with the way the models are constructed (which wasn't done by me). Could anyone suggest how to modify the above code so I could insert Switches and Stacks using get_or_create()?

,这里是堆栈和从/buildings/models.py切换:

And, here is stack and switch from /buildings/models.py:

class Switch(models.Model):
stack = models.ForeignKey(Stack, null=True, related_name='switches')
number = models.PositiveIntegerField()
ip = models.IPAddressField()
num_ports = models.PositiveIntegerField()

def __unicode__(self):
    return unicode(self.number)

@models.permalink
def get_absolute_url(self):
    return ('buildings.views.switch', (self.stack.building.number, self.stack.number, self.number))


class Stack(models.Model):
    building = models.ForeignKey(Building, related_name='stacks')
    number = models.PositiveIntegerField()
    num_switches = models.PositiveIntegerField()

def __unicode__(self):
    return unicode(self.number)

@models.permalink
def get_absolute_url(self):
    return ('buildings.views.stack', (self.building.number, self.number))


推荐答案

我需要导入交换机和堆栈。我更改了:

I needed to import Switch and Stack. I changed:

from buildings.models import建筑物
.models import Building,Switch,Stack

这篇关于如何使用Django模型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆