These are chat archives for django/django

14th
Oct 2018
fsecada01
@fsecada01
Oct 14 2018 01:45
Is there a reason why you need your model fields to include foreign keys? NoSQL databases are mostly for document-based data management use-cases. I use it for querying.
Corey Vincent
@CoreyVincent
Oct 14 2018 15:27
@fsecada01 I want the fields in the model to allow the user to select options that have already been used.
For example:
class College(models.Model):
    name = models.Charfield(max_length=40)

class CollegeClass(models.Model):
    classNumber = models.CharField(max_length=40)
    teacherName = models.CharField(max_length=40)

class CollegeSport(models.Model):
    name = models.CharField(max_length=40)
    coach = models.Charfield(max_length=40)

class Student():
    name = models.CharField(max_length=40)
    college = models.ForeignKey(College, on_delete=models.CASCADE)
    collegeClass = models.ForeignKey(CollegeClass, on_delete=models.CASCADE)
    collegeSport = models.ForeignKey(CollegeSport, on_delete=models.CASCADE)
fsecada01
@fsecada01
Oct 14 2018 15:29
So basically, you would just declare the fields that would feed into the ES model, and then it should work. It doesn't incorporate PKs into the shard process because it is not particularly relevant for querying or other such use cases.
Corey Vincent
@CoreyVincent
Oct 14 2018 15:29
@fsecada01 So, if a CollegeClass has already been created by another student, then the student could select that. Otherwise they could add a new one. But I want to be able to make an advanced search to search through the students information.
Do you know of any good tutorials or documentation for the elasticsearch? I'm thinking I just don't understand it enough to use it.
It doesn't seem to work with foreignKeys. When I do a search, it seems to find the PK, not the name field of the model that is a foreignKey.
Corey Vincent
@CoreyVincent
Oct 14 2018 15:32
@fsecada01 Also, thank you for your help. This whole process is more complicated than I was thinking. I've done similar things in Rails and had no issues.
fsecada01
@fsecada01
Oct 14 2018 15:33
of course! I, too, was in the woods about this once and I appreciate how frustrating it can be.
Corey Vincent
@CoreyVincent
Oct 14 2018 15:33
@fsecada01 Thanks for the link.
you can also try this from pypi: https://pypi.org/project/elasticsearch-django/
basically, follow through the documentation and things should work fine.
Corey Vincent
@CoreyVincent
Oct 14 2018 15:34
Cool, I am using the rest-framework, so the second one may be good.
MilanRgm
@MilanRgm
Oct 14 2018 15:39
class MyUserManager(BaseUserManager):
    """
    A custom user manager to deal with emails as unique identifiers for auth
    instead of usernames. The default that's used is "UserManager"
    """
    def _create_user(self, email, password, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('The Email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self._create_user(email, password, **extra_fields)


class CustomUser(AbstractUser):

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    first_name = models.CharField(max_length=120, blank=False, null=False)
    last_name = models.CharField(max_length=120, blank=False, null=False)

    objects = MyUserManager()

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

    def __str__(self):
        return self.username


CustomUser._meta.get_field('email')._unique = True
when i try to create super user i get the following error.
django.db.utils.IntegrityError: UNIQUE constraint failed: accounts_customuser.username
I cleaned up all the migrations and also deleted database file and then fresh migrate but still i am getting this issue.
fsecada01
@fsecada01
Oct 14 2018 15:49
what is the full error message
also, are you importing AbstractUser from django or is it a custom abstract model?
MilanRgm
@MilanRgm
Oct 14 2018 15:50
I am importing AbstractUser
fsecada01
@fsecada01
Oct 14 2018 15:50
what happens if you put the USERNAME_FIELD into a tuple or a list
?
MilanRgm
@MilanRgm
Oct 14 2018 15:51
Do u mean to say USERNAME_FIELD = ('email', )?
fsecada01
@fsecada01
Oct 14 2018 15:51
also, does AbstractUser have a username field? You are calling it from the class __str__ function.
yes
MilanRgm
@MilanRgm
Oct 14 2018 15:52
I think some error was shown when I used tuple before
this error is shown when using tuple
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: CustomUser has no field named '('email',)'
i changed username to email in __str__. I forgot to replace username to email.
but that should show another error not this error, right?
fsecada01
@fsecada01
Oct 14 2018 15:54
well, switch the USERNAME_FIELD value back to a string value instead of a tuple and check again
MilanRgm
@MilanRgm
Oct 14 2018 15:54
I have used AbstractUser and BaseUserManager from django models
from django.contrib.auth.models import AbstractUser, BaseUserManager
still getting the same error
fsecada01
@fsecada01
Oct 14 2018 15:56

I just did a quick google check, and it seems that the error comes from an issue with migrations.

https://stackoverflow.com/questions/29373887/django-db-utils-integrityerror-unique-constraint-failed-rango-category-new-sl

basically, fake the initial migration and then go through the regular process
MilanRgm
@MilanRgm
Oct 14 2018 15:56
i found similar type so i deleted db.sqlite3 and all the migrations and freshly migrated but still I am getting this issue.
if it's a migration issue, should not this issue be solved if i deleted the database and all the migrations file and re migrate it ?
fsecada01
@fsecada01
Oct 14 2018 15:59
It should be addressed, yes, unless your current environment is not processing changes to your applications. What happens when you run makemigrations with your specific app folders?
MilanRgm
@MilanRgm
Oct 14 2018 15:59
--fake migration says no changes detected
right now No changes detected in app 'accounts'
fsecada01
@fsecada01
Oct 14 2018 16:00
and when you run migrate, are all the datatables reproduced?
MilanRgm
@MilanRgm
Oct 14 2018 16:01
yes when i did it initially
fsecada01
@fsecada01
Oct 14 2018 16:05
I think looking at your full models file may be needed to really understand what's happening. My thinking is that you're customizing the user manager class is somehow interfering with how the user object is inheriting from the base user class. If I were you, I'd try to eliminate the custom code and keep it vanilla, just to get it working. After that, add back the custom code in piecemeal. Hopefully you'll find where your error is and you can address from there.
AJ
@ajostergaard
Oct 14 2018 16:06
@MilanRgm Are you sure you want to subclass AbstractUser and not AbstractBaseUser as in the documented example (https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example)?
MilanRgm
@MilanRgm
Oct 14 2018 16:10
I will try the same way
I think, if i need to change the behavior of authentication, then its best to use AbstractBaseUser.
I am not sure but i think USERNAME_FIELD is not working for AbstractUser
MilanRgm
@MilanRgm
Oct 14 2018 16:18
I have not tried AbstractBaseUser yet so I am asking this question. If I use AbstractBaseUser, will i have field like lastLogin etc or I have to define all the fields by myself.
MilanRgm
@MilanRgm
Oct 14 2018 16:29
class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = CustomUser
        fields = ('email', 'first_name', 'last_name', )
username was listed in fields which I removed and it's working now.
Thanks everyone for your concern and helping nature
AJ
@ajostergaard
Oct 14 2018 16:53
Congrats!