These are chat archives for django/django

14th
Jun 2018
Luke-VideoLibre
@LukeVideo
Jun 14 2018 08:01
@int-elligentSam did you look at signals ?
You could triggers envents on login / logout. Then you just get your timestamp and an id... Then it's up to you.
prinxy
@prinxy
Jun 14 2018 08:45

hello all, i created a custom user model in which i also created a custom UserManager object objects = UserManager in my models.
when i run python manage.py shell and import my User class and do something like User.objects.all( ) everything works perfectly fine including User.objects.create( )
but when testing that same User model, i did User.objects.create( ) and i get an error saying AttributeError: type object User has no attribute objects
Please guys i've been stucked on this since noon...

Hello guys i through up this question yesterday... Just want to inform that i got the solution already...
The issue was that i named my TestCase class same name with my Model class....

# models.py
class new_object( models.Model)

tests.py
class new_object( TestCase ) # all i did to solve the problem was to change this class name

that was the issue

Thanks all
Luke-VideoLibre
@LukeVideo
Jun 14 2018 09:45
OK i got my messages working but i'm still not sure what or why.
Luke-VideoLibre
@LukeVideo
Jun 14 2018 10:09
class Home(generic.TemplateView):
    model = Article
    template_name = 'pages/home.html'
    form_class = ContactForm
    latest_articles = Article.objects.order_by('-date')[:2]


    def get(self, request, *args, **kwargs):
        form = self.form_class()
        latest_articles = self.latest_articles
        return render(request, self.template_name, {'form': form, "latest_articles":latest_articles})


    def post(self, request, *args, **kwargs):
        form = self.form_class()

        print('before form is valid')
        if form.is_valid():
            print('in form is valid')
            messages.success(request,'Mesage envoyé.')
            # return HttpResponseRedirect('/')
        latest_articles = self.latest_articles
        messages.success(request,'Votre message a bien été envoyé.')
        print('after form is valid')
        return render(self.request, self.template_name, {'form': form, "latest_articles":latest_articles})
The if form.is_valid is never True. Most exemples show this method to get your message. Maybe class view doesn't work this way?
Siddharth Jain
@singhaisid0
Jun 14 2018 10:38
i have imported a project in python2.7 environment and now getting error No module named object_tools
Please help me as there is no module name object_tools in python
Luke-VideoLibre
@LukeVideo
Jun 14 2018 10:42
Install that.
prinxy
@prinxy
Jun 14 2018 10:59

i am getting this error message when running my test

users.models.DoesNotExist: User matching query does not exist.

that error is pointing to user = User.objects.get( pk = 1 ).

Error signifying that i'm trying to access a record that is not in database.
i have a setUp method for the TestCase though, but don't know why that error keeps coming

Things I've Done

since i'm trying to access record that doesn't exist even when i've created it in my setUp( ) method,
i then try to recreate that same record using my helper method and, i got another error saying
cannot have duplicate unique key in table 'pk = 1'

can someone please help me out on this

prinxy
@prinxy
Jun 14 2018 11:17

----- I don't understand ------

trying to retrieve record with user = User.objects.get( pk = 1 ) i get an error
user matching query does not exit

--
but retrieving that same record with user = User.objects.get( )
everything worked as expected...

Please guys i don't understand

prinxy
@prinxy
Jun 14 2018 11:26
sorry guys now i understand,
i don't know if i'm fully correct
for each test method, a record is created and deleted,
so trying to access a deleted record, will result to User matching query does not exist

if i'm correct, then to retrieve a record from a db within test method it's best to do
user = User.objects.get()

Please someone should correct me if i'm wrong

garynobles
@garynobles
Jun 14 2018 11:34

For my next 2 challenges:
(1) drop down menus in django-filter and
(2) filtering on related fields .
The code snippit:

<div class="form-group col-sm-4 col-md-3">
{{ filter.form.container_name.label_tag }}
{% render_field filter.form.container_name class="form-control" %}
</div>

<div class="form-group col-sm-4 col-md-3">
{{ filter.form.location_id.label_tag }}
{% render_field filter.form.location_id class="form-control" %}
</div>

this location_id references the location table (location_id) but I would like to (1) have a drop down listing all the unique container_name values and (2) filter on the location_name (via the fk with location_id) I've been googling but I'm not getting anywhere fast!

Chris Goodwin
@hannylicious
Jun 14 2018 11:40
@prinxy if your setUp for your test class, are you creating a user?
prinxy
@prinxy
Jun 14 2018 11:40
@hannylicious yes
Chris Goodwin
@hannylicious
Jun 14 2018 11:42
Are you just creating one user?
prinxy
@prinxy
Jun 14 2018 11:42
yes
Chris Goodwin
@hannylicious
Jun 14 2018 11:43
couldn't you just get them all then and use .first() and not worry about it?
YourModel.objects.all().first()
same thing if you are only creating one user, then you don't have to worry about it.
prinxy
@prinxy
Jun 14 2018 11:44
thanks for the hint @hannylicious...
thanks man
Chris Goodwin
@hannylicious
Jun 14 2018 11:48
It's worth noting I think (and someone else can correct me if I'm wrong): but I think that when writing tests, you want your queries and stuff to be as generic as possible. You want to avoid using specific PK's and things, because if (for some reason) those didn't exist or failed to exist - your test would break and be inaccurate. By making them more generic (like using .all().first()) you're not tied to a specific record but it's more generic and should allow testing in a wider array of environments/scenarios and give you valid results.
prinxy
@prinxy
Jun 14 2018 11:50
I agree @hannylicious
i just checked out the first( ) query method on the official doc.... and it's working like magic for me
@hannylicious
thanks
Chris Goodwin
@hannylicious
Jun 14 2018 11:54
No problem ! :clap:
Thinnappan.K
@thinnappan
Jun 14 2018 14:29
hello guys pls help me how to install Django framework!
Chris Goodwin
@hannylicious
Jun 14 2018 14:32
garynobles
@garynobles
Jun 14 2018 19:23

I'm trying filtering and pagination, the following should do both but I can onlt get the pagination to work.
I have this view:

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from . import filters

def listing(request):
    filtered_qs = filters.SampleFilter(request.GET,queryset=Samples.objects.all()).qs
    paginator = Paginator(filtered_qs, 15)
    page = request.GET.get('page')
    try:
        response = paginator.page(page)
    except PageNotAnInteger:
        response = paginator.page(1)
    except EmptyPage:
        response = paginator.page(paginator.num_pages)
    return render(request,'search/page.html',{'response': response})

In the template I have:

<div class="form-group col-sm-4 col-md-3">
        {{ response.material.label_tag }}
        {% render_field filter.form.material class="form-control" %}
      </div>

{% for samples in response %}
<tr>
  <td>{{ samples.sample_id }}</td>
  <td>{{ samples.area_easting }}</td>
  <td>{{ samples.area_northing }}</td>
  <td>{{ samples.context_number }}</td>
  <td>{{ samples.sample_number }}</td>
  <td>{{ samples.material }}</td>
  <td>
    <div class="btn-group" role="group" aria-label="Basic example">
  <a href="{% url 'editsamplesearch' pk=samples.pk %}" class="btn btn-primary" role="button">edit</a>
  <a href="{% url 'editsamplesearch' pk=samples.pk %}" class="btn btn-secondary" role="button">take out</a>
  <a href="{% url 'editsamplesearch' pk=samples.pk %}" class="btn btn-secondary" role="button">request</a>
  </div>
  </td>
</tr>
{% empty %}
<tr>
  <td colspan="5">No data</td>
</tr>
{% endfor %}

The normal pagination code follows this and works fine, sorry for the long post, but I need to get this working by before Monday since its for an archaeological excavation, if we can't get this to work we will be pretty stuck! All help appreciated.