当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Django view - exclude in queryset?

问题描述:

I have a problem:

After creating new world, (all countries are automatically assigned to every new world) user choose one country which will showed on top of the site and he can't delete this country. How to do this?

class Country(models.Model):

name = models.CharField(verbose_name="Name of country", max_length=100, default="Australia")

number = models.IntegerField(verbose_name="number of country", default="1")

def __unicode__(self):

return self.name

class World(models.Model):

country = models.ManyToManyField(Country)

name = models.CharField(verbose_name="New Map", max_length=100)

def __unicode__(self):

return self.name

Now i have this: after creating world user is redirecting to new_my_world, there he should choose his country and be redirected to site with my_world view where he can delete countires expect this one. How to do this?

views.py

def new_my_world(request, pk):

world = get_object_or_404(World, pk=pk)

return render(request, 'game/new_my_world.html', {'world': world})

def my_world(request, pk):

world = get_object_or_404(World.objects.exclude(??, pk=pk)

return render(request, 'game/my_world.html', {'world': world})

def choose_country(request, world_pk, country_pk):

world = get_object_or_404(World, pk=pk)

country = get_object_or_404(Country, pk=pk)

return redirect('my_world', pk=world.pk)

I know in my_world view there's should be something with exclude (excluding this coosen country) but i don't know how to connect it.

edit

url:

 url(r'^world/(?P<pk>[%&+ \w]+)/(?P<country_pk>[%&+ \w]+)/$', views.my_world, name='my_world'),

edit2:

How create url to my_world? After creating new world,when i click on first country to choose it i get an error

with this url:

url(r'^world/(?P<pk>[%&+ \w]+)/(?P<country_pk>[%&+ \w]+)/$', views.my_world, name='my_world'),

Page not found (404)

Request Method: GET

Request URL: http://127.0.0.1:8000/world/22/9/

Raised by: game.views.my_world

No World matches the given query.

网友答案:

I fix choose_country to the following:

def choose_country(request, world_pk, country_pk):
    world = get_object_or_404(World, pk=world_pk)
    country = get_object_or_404(Country, pk=country_pk)
    return redirect('my_world', pk=world.pk, country_pk = country.id)

Then in your my_world function it should be changed like this:

def my_world(request, pk, country_pk):
    world = get_object_or_404(World.objects.exclude(country__id = country_pk), pk=pk)
    return render(request, 'game/my_world.html', {'world': world})

Edit 1 if the country is the same of as the country of my world, the above get_object_or_404(World.objects.exclude(country__id = country_pk), pk=pk) will for sure return Not Found. So I suggest that my_world to be as follows:

def my_world(request, pk, country_pk):
        world = get_object_or_404(World, pk=pk)
        countries = get_object_or_404(Country.object.exclude(pk=country_pk))
        return render(request, 'game/my_world.html', {'world': world, 'countries': countries})
分享给朋友:
您可能感兴趣的文章:
随机阅读: