开发者

Django Application zum loggen

开发者 https://www.devze.com 2023-04-12 13:24 出处:网络
Let\'s say I have Two Models: class Thinclient(models.Model): hostname = models.TextField(_(\'hostname\'),unique=True, \\

Let's say I have Two Models:

class Thinclient(models.Model):
    hostname = models.TextField(_('hostname'),unique=True, \
        editable=False)
    logs = models.ManyToManyField(Log, blank=True, null=True)

class Log(models.Model):
    logname = models.TextField(editable=False)
    created = models.DateTimeField(auto_now_add=True, editable=False)

As you can see I have thin clients. Now If one of those thin clients boots it is supposed to send a POST request to my app with the thin client name and a logname (i.e. "booting ...").

Now my view will handle all the work and that's where I have my problem. It currently looks like this:

def log(request):
    if request.method == 'POST':
        form = ThinclientForm(request.POST)
        if form.is_valid():
            message = form.cleaned_data['logname']
            Log.objects.get_or_create(logname=message)
            return HttpResponse(content="", mimetype=None, status=200)
        else:
            return HttpResponse(content="Unsuccessful", mimetype=None,
                status=400)

        return render_to_response('thin/status', {
            'form': form, })

However that won't wok because I have to assign 开发者_运维问答a message to one specific thin client. I suppoose I have to write my own form with hostname, logname and that is where i have my problem how can I save my models in a way that the message is assigned to a thin?

I hope I could explain what I need to know, if not tell me. And Thanks for any help in this


Assuming your Thinclient name is also included in the POST, you can just get it from there and use it to look up the actual object, then assign that Log message to it.

log = Log.objects.get_or_create(logname=message)
client = Thinclient.objects.get(hostname=request.POST['clientname']
client.logs.add(log)

(One note: you shouldn't use TextFields for things like client names - these are stored as BLOB/TEXT objects in the database, which is far less efficient than normal VARCHARs. Use CharField instead.)


There are several problems here. The first is in the design of your models. The link from Log to ThinClient should be a ForeignKey from Log to ThinClient

class Thinclient(models.Model):
    hostname = models.TextField(_('hostname'),unique=True, \
        editable=False)

class Log(models.Model):
    thin_client = models.ForeignKey(Thinclient)
    logname = models.TextField(editable=False)
    created = models.DateTimeField(auto_now_add=True, editable=False)

Do you even need a form here? Aren't your thin clients just doing a post to this url? Are they really doing a GET to get the form? The benefit of forms is the ability to turn them into HTML and data validation. I don't think you need either of those here.

The host name of the remote client is stored in the REMOTE_HOST request header so you can use this to pull it out.

def log(request):
    thin_client, _ = Thinclient.get_or_create(hostname=request.META["REMOTE_HOST"])

    Log(thin_client=thin_client, logname=request.POST["logname"]).save() 

    return HttpResponse(content="OK", mimetype="text/plain")

As a side note you should always return some content. Some proxies do not like zero byte responses. You should also always specify a mimetype, even if it's the default of text/html.

0

精彩评论

暂无评论...
验证码 换一张
取 消