0

Just for testing, I have a Python that scripts that enters IP addresses into the database.

I have the following values:

127.0.0.1  
127.0.0.1  
10.42.1.42  
10.42.1.42  
10.42.1.42  

Model:

class Invalid(models.Model):
    access = models.DataTimeField()
    ip = models.CharField()
    login = models.CharField()

In the template, this is what I have for the drop down list:

<p>Choose: 
<select>
{% for item in results %}
    <option value="{{ item.ip }}">{{ item.ip }}</option>
{% endfor %}
</select>
<button type="button">Filter</button>  
</p>  

However, the problem with this method is that it lists all the values. As far as I'm aware, there is no way to create an array/lists although I may be mistaken.

Is there any way to only have distinct values show up in the list, that is:

127.0.0.1  
10.42.1.42
halfer
  • 19,824
  • 17
  • 99
  • 186
gjvatsalya
  • 1,129
  • 13
  • 29
  • You really don't give enough details on what you are trying to do to be able to give a clear answer. What does your model (and database table) look like for the IPs? – tatlar Jun 26 '14 at 03:43
  • Sorry, I should've specified. My model's name is Invalid, with the fields: "access" (DateTimeField), "ip" (CharField), "login" (CharField). – gjvatsalya Jun 26 '14 at 13:10
  • Look at the Django [Model reference](https://docs.djangoproject.com/en/dev/ref/models/fields/) and my answer below for which field types to use. You should use `IPAddressField()` for your `ip` field at the very least. I have edited your post to reflect this. It will need to be approved by a moderator for it to show up. – tatlar Jun 26 '14 at 22:41

2 Answers2

0

assuming results is a queryset :

If you have a backend that supports (postgres) it you could use distinct on the ip field

results.distinct('ip')


Select DISTINCT individual columns in django?


Maybe you could also build a unique collection in python

uniques_ips = {}
for item in results:
  if item.ip not in unique_ips:
     unique_ips[item.ip] = item

unique_ips.values()
Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145
  • Sorry, I'm new to Django. How would I access the values stored in the python list. Should I create a new table for the unique IPs? – gjvatsalya Jun 25 '14 at 21:44
0

In your views.py file you would subset your queryset to get the distinct values only. Then you would only return these distinct values to your template.

So, using your example, you have a context object called results. You don't define how you came to populate that object, or what your model looks like, so lets assume the following model for your application for the sake of argument:

from django.db import models

class MyModel(models.Model):
    ip = models.IPAddressField()

Now, lets take a look at views.py and here we are using Generic Class Based Views:

from django.views.generic.list import ListView

class IpListView(ListView):
    def get_queryset(self):
        queryset = super(IpListView, self).get_queryset()
        unique_ips = queryset.values('ip').distinct()
        return unique_ips

Note: You could also put the distinct() method above directly into the urls.py queryset object below...

In your urls.py you would make sure you route IpListView to your template. Something like:

from django.conf.urls import patterns, url
from myapp.models import MyModel
from myapp.views import IpListView

urlpatterns = patterns('',
    url(r'^$',
        IpListView.as_view(
            queryset=MyModel.objects.order_by('ip'),
            context_object_name='results',
            template_name='myapp/mytemplate.html',
        ), name='mymodel-ips'
    )
)

Note here how results is your context object name. This means that all your ip field values will be available in your template mytemplate.html as:

for r in results:
    ip_option = r.ip

And because you have already subset the queryset to only have the unique IPs, you get what you needed. So you just need to loop through:

<p>Choose: 
    <select>
    {% for item in results %}
        <option value="{{ item.ip }}">{{ item.ip }}</option>
    {% endfor %}
    </select>
    <button type="button">Filter</button>
</p>

One caveat here: you don't state if you need any other fields from your model. If you do, the queryset I listed above will modify the results you get, and you might not get back what you expect if you wanted to use other values entered to be returned...

tatlar
  • 3,080
  • 2
  • 29
  • 40